SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Forcing the download of a SharePoint Document

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. 

  1. Using SharePoint Designer, put this code in the code behind for a page in the site.
  2. 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
  3. 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

Comments

Links (12/21/2008) « Steve Pietrek - Everything SharePoint wrote Links (12/21/2008) « Steve Pietrek - Everything SharePoint
on 12-21-2008 6:19 PM

Pingback from  Links (12/21/2008) « Steve Pietrek - Everything SharePoint

chizzel wrote re: Forcing the download of a SharePoint Document
on 02-25-2009 5:55 AM

jscott,

If I'd only found this code a few days ago it would have saved me a lod of trouble. Nice Work!! I had all the code except the  Response.BinaryWrite(spFile.OpenBinary()); which I wasn;t aware off. Thanks

Chizzel

Jens Gyldenkærne Clausen wrote re: Forcing the download of a SharePoint Document
on 05-26-2009 4:28 PM

Could this method be applied to a document library so all documents within the library are delivered with the force-download header?

jscott wrote re: Forcing the download of a SharePoint Document
on 05-26-2009 6:54 PM

Jens,

Off the top of my head, I think you could create a URL column in your document library and create a simple workflow in SharePoint designer (or in Visual Studio if you want to reuse it) that sets the URL to the custom page with the force download code in it and you can specify the URL parameter.  You could set the text portion of the URL to be the file name and it would be practically transparent to the end user.  

An HTTP Module might be able to do the trick.  I've never tried it with a document... yet.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.