1

I'm having problems with FileResult returning a file with a specific filename. In the database the filename is just and ID + extension (e.g: 456789.mp3)

This piece of code

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(fs, "application/octet-stream", "myfile.mp3");

Work well in every browser except Webkit browsers (Chrome, Safari). Chrome and Safari receive files as original filename (456789.mp3). When I add headers with

Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.mp3");

Safari receives the file as myfile.mp3attach (notice "attach" appended to the extension?), however Chrome receives this file as myfile.mp3,attach (it appends ",attach" to the extension)

Has anyone experienced this kind of a problem?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
ShyGuy82
  • 115
  • 2
  • 6

2 Answers2

7

It has helped me:

        var encoding = System.Text.Encoding.UTF8;
        Response.Charset = encoding.WebName;
        Response.HeaderEncoding = encoding;

            Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", (Request.Browser.Browser == "IE") ? HttpUtility.UrlEncode(fileName, encoding) : fileName));

        return File(fs, contentType, fileName);
Andy Clarke
  • 3,234
  • 5
  • 39
  • 66
user588638
  • 71
  • 1
  • 3
0

Putting

         Response.Clear();
         Response.ClearHeaders();

Before

         Response.Charset = encoding.WebName; 

solved this for me

tedbaker
  • 31
  • 7