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