none
Forcing the download of a SharePoint Document RRS feed

  • Question

  • I have a requirement to forcing the download a fillable PDF form (PDF version 10) instead of opning it in the browser. My enviornment is SharePoint 2007 (MOSS 2007). So I added an ASP page using SharePoint Designer, added code as described in this link.

    http://vspug.com/jscott/2008/12/19/forcing-the-download-of-a-sharepoint-document/

    This works fine no issue. When user click the link the Save as dialog popped up and PDF saves correctly. When I have created a webpart for the same code for same functionality it works fine with only older version of PDF documents. But it fails for the new fillable PDF document. However the document saved but the file is corrpted while downloading and saving process. When I open the save file at the destination location, it does not open with error message crrupted file.

    Any help on this will be hightly appreciated. Thanks in advance.

    Tayyab  

    Monday, February 14, 2011 7:09 PM

Answers

  • Hi everyone,

     

    i was having the exact same problem that a demand made it necessary to force a download window for all kind of MIME type, e.g. images.

    The difference is that i needed it to work for Office 365 / SharePoint Online environments as well.

    As you might know, deploying application pages as sandboxed solutions is not possible, so the proposed answer was no solution for me.

    I found out that, at least for SharePoint 2010, there exists an application page in the Layouts-Folder called "download.aspx" that does (beneath some other functionality) that exact thing.

    After a little reflector digging, the parameter was obvious as well: just call it with the parameter "SourceUrl", like that:

    /_layouts/download.aspx?SourceUrl=fileurl

    where fileurl can be either an absolute or site collection relative url.

     

    Quite easy ;)

    Felix

    Friday, October 7, 2011 11:25 AM
  • Hi Trana_ca,

    Thank you for posting and we are glad to help with you.

    >>When I have created a webpart for the same code for same functionality it works fine with only older version of PDF documents. But it fails for the new fillable PDF document.

    I studied the code snippet in the article:

    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(); 
    }
    
    

    Could it be the OpenBinary() method failed when the size of the new filled PDF document is 0? You can check it in your program. Please take a look at the MSDN article about SPFile.OpenBinary method: http://msdn.microsoft.com/en-us/library/ms477413.aspx

    I hope it can help you and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Friday, March 4, 2011 3:43 AM
    Tuesday, February 15, 2011 6:41 AM

All replies

  • Hi Trana_ca,

    Thank you for posting and we are glad to help with you.

    >>When I have created a webpart for the same code for same functionality it works fine with only older version of PDF documents. But it fails for the new fillable PDF document.

    I studied the code snippet in the article:

    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(); 
    }
    
    

    Could it be the OpenBinary() method failed when the size of the new filled PDF document is 0? You can check it in your program. Please take a look at the MSDN article about SPFile.OpenBinary method: http://msdn.microsoft.com/en-us/library/ms477413.aspx

    I hope it can help you and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Friday, March 4, 2011 3:43 AM
    Tuesday, February 15, 2011 6:41 AM
  • Hi Trana_ca,

    Have you fixed issue yet? Does the suggestion work for you or not? If you still show any concern on the issue, just feel free to follow up and we are glad to help with you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 17, 2011 7:35 AM
  • Thanks Bruce!

     

    I have resolved this issue some times back and did not checked back the thread. Today I was looking some thing else and I saw your response. Sorry about that and thanks any way.

    This code is fine and works as expected.

     

    Tayyab

    Monday, August 15, 2011 8:33 PM
  • Hi everyone,

     

    i was having the exact same problem that a demand made it necessary to force a download window for all kind of MIME type, e.g. images.

    The difference is that i needed it to work for Office 365 / SharePoint Online environments as well.

    As you might know, deploying application pages as sandboxed solutions is not possible, so the proposed answer was no solution for me.

    I found out that, at least for SharePoint 2010, there exists an application page in the Layouts-Folder called "download.aspx" that does (beneath some other functionality) that exact thing.

    After a little reflector digging, the parameter was obvious as well: just call it with the parameter "SourceUrl", like that:

    /_layouts/download.aspx?SourceUrl=fileurl

    where fileurl can be either an absolute or site collection relative url.

     

    Quite easy ;)

    Felix

    Friday, October 7, 2011 11:25 AM
  • This worked great for me. Thanks!

    /_layouts/download.aspx?SourceUrl=fileurl

    • Edited by Huxly Friday, January 24, 2014 7:11 PM
    Friday, January 24, 2014 7:10 PM
  • Hi Guys,

    I have the same requirement as the first poster but with a excel document.

    This link  http://vspug.com/jscott/2008/12/19/forcing-the-download-of-a-sharepoint-document/    did not work,

    does any one have a new link for me, as i need to implement something like this in a SharePoint document library at work.

    Monday, July 3, 2017 10:06 AM
  • Hi Felix,

    I had the same requirement and this worked perfectly fine for xlsx, but not for xlsm. Do you know any work arounf for downloading the xlsm files ?

    Thanks in advance

    Friday, May 4, 2018 9:27 AM