Recently, I implemented MOSS 2007 as a web content manager for a client's public website. One of the things they wanted was the ability to force the "Save As..." dialog for certain documents (in this case, they were media files).
The requirements were to get this working for Internet Explorer 7 and FireFox 2 and above, although I think this will work for most if not all the browsers. We tested on a couple others but I don't remember for sure if Chrome and/or Safari were on that list.
The code below assumes that the web path to the file that needs to be downloaded is passed in via the 'file' URL parameter.
void Page_Load(object sender, System.EventArgs e)
{
if (Request.QueryString["file"] != null)
DownloadFile(Request.QueryString["file"].ToString());
}
private void DownloadFile(string fileName)
{
string path = System.Web.HttpContext.Current.Server.MapPath( fileName );
string name = System.IO.Path.GetFileName( path );
Microsoft.SharePoint.SPFile spFile =
Microsoft.SharePoint.SPContext.Current.Web.GetFile(fileName);
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType =
"application/force-download";
Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
Response.BinaryWrite(spFile.OpenBinary());
Response.End();
}
Off the top of my head, I can think of 3 different ways you could use this code.
- Using SharePoint Designer, put this code in the code behind for a page in the site.
- Create a page in visual studio, use a web deployment project to compile it to a DLL, Deploy the web page somewhere under the _layouts folder, and deploy the DLL to the Bin or the GAC. See http://www.sharepointblogs.com/jscott/archive/2008/04/15/integrating-asp-net-2-0-web-pages-into-sharepoint-2007.aspx for more details
- Create a HttpModule that will execute the above code.
To keep it simple for the non-technical content administrators, we went with option 1. This option is my least favorite technically but I think the easist for the end user. We create a SaveFile.aspx page in SharePoint designer and stuck it in the top most level of the root site (NOT in the pages library).
So the file is at www._company_.com/SaveFile.aspx.
We then would have the content administrators link to the document like they normally would throught the publishing GUI. The URL would be something like, www._company_.com/site1/Documents/Medai.wmv
Then we would have them insert /SaveFile.aspx?file= right after www._company_.com resulting in www._company_.com/SaveFile.aspx?file=/site1/Documents/Medai.wmv
Hope this helps
Posted
12-19-2008 1:09 PM
by
jscott