Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 ?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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