代码之家  ›  专栏  ›  技术社区  ›  Ian Kemp

Firefox忽略Response.ContentType

  •  4
  • Ian Kemp  · 技术社区  · 16 年前

    我有以下代码,基本上是“代理”一个文件从一个服务器到另一个服务器。它在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();
      }
    }
    
    5 回复  |  直到 16 年前
        1
  •  6
  •   Loris    16 年前

    尝试添加 Response.ClearHeaders() 打电话之前 ClearContents() 如x2所述,将文件作为 application/octet-stream :

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

    当我需要将可下载文件(不一定是MP3)传输到客户机时,它对我很有用。

        2
  •  3
  •   JeremyWeir    16 年前

    如果你还没有这样做的话,我的第一步将是使用 Live HTTP Headers Firefox插件,或 fiddler 以确保他们符合你的期望。

        3
  •  0
  •   Brad Bruce    16 年前

    典型的类型是音频/MP3。你看到什么问题了?

    还有一个联系 here 关于MP3文件的QuickTime劫持。

        4
  •  0
  •   devio    16 年前

    如果只使用response.clear()而不是response.clearcontent(),结果是否相同?

        5
  •  -1
  •   x2. alexey    16 年前

    我不确定,但也许清除标题和添加mime类型可以帮助

    Response.ClearHeaders();
    Response.AddHeader("MIME Type",Utilities.GetContentType(audioFileInfo));
    
    推荐文章