1

I have created a function where a user can download a pdf file from my webpage. The file is stored in a databse and is requested from a webapi. The return value of the webapi is a byte[].

My issue here is that when i run the web application on my local iis this function runs without any errors. I get the pdf file and it is downloaded correctly on my machine. But when i deploy the web application to my Test server this code generates either RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION in chrome with some of the files where as other files are downloaded to the machine but when i try to open the pdf file i get: could not load the pdf file.

This happens with both chrome and IE.

This is my code:

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    this.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    var result = File(translationFile.FileContent.Content, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
    return result;
}

I have been trying to fix this issue for 2 days now but i simply cant figure out what the issue is. Hope you guys can help. Thanks.

Lahib
  • 1,305
  • 5
  • 34
  • 62

1 Answers1

2

You don't need to use Content-Disposition. .Net will add it for you. From the docs.

The fileDownloadName parameter is used to generate the content-disposition header. The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed. The MediaTypeNames class can be used to get the MIME type for a specific file name extension.

I tend to use the Stream-overload:

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    var stream =  = new MemoryStream(translationFile.FileContent.Content);
    return File(stream, "application/pdf", fileName);
}

But you can use the byte[] as well:

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    return File(translationFile.FileContent.Content, "application/pdf", fileName);
}

EDIT: If you got an error when opening the PDF you can ensure that the web browser is doing the right thing by manually saving the PDF from code as well. If that file has errors as well you're probably generating an incorrect byte[].

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    var stream =  = new MemoryStream(translationFile.FileContent.Content);

    // Code for debugging
    var tempDir = "C:\\temp"; // Make sure app pool can write here.
    var path = Path.Combine(tempDir, fileName); // Possibly add extension here.
    using (var fileStream = File.Create(path))
    {
        stream.Seek(0, SeekOrigin.Begin);
        stream.CopyTo(fileStream);
    }
    stream.Seek(0, SeekOrigin.Begin);

    // Return to client.
    return File(stream, "application/pdf", fileName);
}
smoksnes
  • 10,509
  • 4
  • 49
  • 74
  • Well that did the trick. Now i can download the file to my HDD. But the file is 1KB and it should be around 800kb. So i get this error on when i try to open the pdf file. http://prntscr.com/bdqv53 This does not happen locally. – Lahib Jun 08 '16 at 06:48
  • Thank you for the help. Much appreciated. – Lahib Jun 08 '16 at 07:03
  • I think i have pinpointed the error to be in the web api that contains the file. i tried entering the url in google postman and i get a response with the dev envoirment. But when insert the url for the test envoirment i get a "Service unavailable" error. – Lahib Jun 08 '16 at 07:40