Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Download file using Web API

public HttpResponseMessage DownloadInvoiceFile(string filePath)
{
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
HttpResponseMessage response= new HttpResponseMessage(HttpStatusCode.OK);response.Content = new StreamContent(stream);response.Content.Headers.ContentLength = stream.Length;response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

string contentType = string.Empty;
string ext = Path.GetExtension(filePath).ToLower();
switch (ext)
{
case ".pdf":      contentType = "application/pdf";
      break;
case ".doc":
case ".docx":      contentType = "application/msword";
      break;
case ".xls":
case ".xlsx":      contentType = "application/vnd.ms-excel";
      break;
case ".txt":      contentType = "text/plain";
      break;
case ".rtf":     contentType = "application/rtf";
     break;
default:
     break;
}response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

return response;

}

Above code works fine in my development machine (downloads all files in all browsers) but I am getting following error while deploying in server. 

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<Error>

<Message>An error has occurred.</Message>

</Error>

Please help.

Best Answer
0 Votes
0 REPLIES 0