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();
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With