0

I've created a web-part. I've added few documents to it. I'm implementing download functionality. I want file download dialog. My code is like this.

DownloadItem(SPListItem item)
{
   // Get physical path of the file
   string itemPath = MapPathSecure(item.File.Url);

   HttpContext.Current.Response.ContentType = "application/x-download";
   HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + item.File.Name);            
   HttpContext.Current.Response.TransmitFile(itemPath);
   HttpContext.Current.Response.End();
}

This throws an error : "Could not find a part of the path 'Path to the document'. The path does not exist. I know that Sharepoint documents are not located on the hard disk. Am I missing something here ?

How do I get this code to work ?

1 Answer 1

0

I slightly modified the code like this and it worked :)

byte[] data = item.File.OpenBinary();           
HttpContext.Current.Response.ContentType = "application/x-download";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + item.File.Name);
HttpContext.Current.Response.BinaryWrite(data);
HttpContext.Current.Response.End();

Thanks alot for this user Upload/Delete/Download from Document Library

1
  • Any idea how to do a page post back after file down load. I have a similar WP documents showing up in a SPGridView. On a LinkButton click i download the document. Only the first download works. So i was thinking to do a page post back afterwards. How to do that ?
    – Shaamil
    Commented Aug 28, 2014 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.