Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox ignores Response.ContentType

I have the following code that's essentially "proxying" a file from one server to another. It works perfectly in IE, but Firefox seems to ignore the Content-Type header and always transmits the files (MP3s) as text/html.

It's not a major issue, but I'd like to get it working correctly in all browsers, so can anyone assist? Also, if there is a better/more efficient way of accomplishing this, please post it!

FileInfo audioFileInfo = new FileInfo(audioFile);
HttpWebRequest downloadRequest = (HttpWebRequest) WebRequest.Create(audioFile);
byte[] fileBytes;

using (HttpWebResponse remoteResponse = (HttpWebResponse) downloadRequest.GetResponse())
{
  using (BufferedStream responseStream = new BufferedStream(remoteResponse.GetResponseStream()))
  {
    fileBytes = new byte[remoteResponse.ContentLength];
    responseStream.Read(fileBytes, 0, fileBytes.Length);

    Response.ClearContent();
    // firefox seems to ignore this...
    Response.ContentType = Utilities.GetContentType(audioFileInfo);
    // ... and this
    //Response.ContentType = remoteResponse.ContentType;
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", audioFileInfo.Name));
    Response.AddHeader("Content-Length", remoteResponse.ContentLength.ToString());
    Response.BinaryWrite(fileBytes);
    Response.End();
  }
}
like image 832
Ian Kemp Avatar asked Mar 01 '23 09:03

Ian Kemp


2 Answers

Try adding a Response.ClearHeaders() before calling ClearContents() like x2 mentioned and sending the file as application/octet-stream:

Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"blah\""); 
...

Works for me when I need to transmit downloadable files (not necessarily mp3s) to the client.

like image 130
Loris Avatar answered Mar 12 '23 03:03

Loris


If you haven't done so already, my first step would be to check out the headers using something like Live HTTP Headers firefox plugin, or fiddler to make sure they what you expect.

like image 41
JeremyWeir Avatar answered Mar 12 '23 04:03

JeremyWeir