Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Edge ignoring content disposition response header's FileName property?

I'm sending a file to the browser to be saved locally. This works fine in all browsers except Microsoft Edge, where it replaces the filename with a guid. The file has to be downloaded with a specific filename, is there an explanation or workaround for this problem? My answer "don't use Edge" will be rejected.

        var response = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(CreateFile(fileContents))
        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = string.Concat(fileName, ".xlsx")
        };
like image 399
Neil Humby Avatar asked Nov 09 '22 08:11

Neil Humby


1 Answers

I had the same problem on Edge, when the user click to download a .ai file it opens automatically as a .pdf file rather than downloading the .ai file.

The ContentDisposition was correct and works very well on all Browsers, but seems that Edge was ignoring this header for some reason.

After long investigations and tryings I ended to 2 solutions:
- Always download your files as Zip files (this was not an option for me and I didn't find it acceptable specially on mobile devices)
- Add the download attribute to the download file link and make sure the attribute value has the file name. (this solved the problem on edge and works fine on all other browsers)

More about the attribute: https://davidwalsh.name/download-attribute

like image 194
Amr Elgarhy Avatar answered Nov 14 '22 21:11

Amr Elgarhy