Firefox忽略Response.ContentType

Ian*_*emp 4 .net c# asp.net httpwebrequest

我有以下代码,基本上"代理"从一个服务器到另一个服务器的文件.它在IE中完美运行,但Firefox似乎忽略了Content-Type标题并始终将文件(MP3)传输为text/html.

这不是一个主要问题,但我希望它能在所有浏览器中正常运行,那么任何人都可以提供帮助吗?此外,如果有更好/更有效的方法来实现这一点,请发布它!

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

Lor*_*ris 6

尝试Response.ClearHeaders()在调用ClearContents()x2 之前添加a 并将文件发送为application/octet-stream:

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

当我需要将可下载文件(不一定是mp3)传输到客户端时,对我有用.