views:

851

answers:

5

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();
  }
}
A: 

I don't sure but maybe clearing headers and adding mime-type can help

Response.ClearHeaders();
Response.AddHeader("MIME Type",Utilities.GetContentType(audioFileInfo));
x2
-1, "MIME Type" is not w3c standard http header.
csharptest.net
A: 

The typical type for this is audio/mp3. What issue are you seeing?

There's also a link here regarding Quicktime hijacking of MP3 files.

Brad Bruce
+4  A: 

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.

Loris
I'm not sure what Utilities.GetContentType(audioFileInfo) is returning, but the Loris' suggestion should work.Response.ContentType = "application/octet-stream";
csharptest.net
+3  A: 

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.

jayrdub
A: 

Is the result the same if you use Response.Clear() instead of Response.ClearContent() only?

devio