【问题标题】:Chrome, pdf display, Duplicate headers received from the serverChrome、pdf 显示、从服务器接收到的重复标头
【发布时间】:2012-01-25 04:56:20
【问题描述】:

我在网站上有一个部分,我在灯箱内显示 pdf。最近的 chrome 升级打破了这个显示:

错误 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): 收到多个 Content-Disposition 标头。这是不允许的 防止 HTTP 响应拆分攻击。

这在 IE 中仍然可以正常工作。

我在 IIS6 上使用 ASP.NET MVC3

我用来生成文件的代码如下。

如果我删除内联语句,则文件会下载,但这会破坏灯箱功能。

问题代码

public FileResult PrintServices()
{
    //... unrelated code removed
    MemoryStream memoryStream = new MemoryStream();
    pdfRenderer.PdfDocument.Save(memoryStream);
    string filename = "ServicesSummary.pdf";

    Response.AppendHeader("Content-Disposition", "inline;");

    return File(memoryStream.ToArray(), "application/pdf", filename);
}

修复

删除

Response.AppendHeader("Content-Disposition", "inline;");

然后改变

return File(memoryStream.ToArray(), "application/pdf", filename);

return File(memoryStream.ToArray(), "application/pdf");

【问题讨论】:

标签: asp.net-mvc-3 google-chrome iis-6


【解决方案1】:

此解决方案将保留文件名并在浏览器中打开文件 (.net mvc)

Response.Headers["Content-Disposition"] = "inline;filename=\"" + theFile.filename + "\"";
return File(filePath, mimeType);//don't specify filename. It will create second Content-Disposition header

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并通过从 return 语句中删除文件名来解决它。

    变化:

     return File(outStream.ToArray(), "application/pdf", "Certificate.pdf");
    

    到:

     return File(outStream.ToArray(), "application/pdf");
    

    并保留:

    Response.AddHeader("content-disposition", "attachment;filename=\"" + "Certificate.pdf" + "\"");
    

    这仍然保留下载文件的名称。

    【讨论】:

      【解决方案3】:

      我的问题是由于双引号引起的,如下所示:

      var encoding = System.Text.Encoding.UTF8;
      
      *Response.AddHeader("Content-Disposition", string.Format("attachment; filename=**\"{0}\"**", HttpUtility.UrlEncode(file, encoding)));*
      

      把上面的改成这个就行了!

      *Response.AddHeader("Content-Disposition", string.Format("attachment; filename=**{0}**", HttpUtility.UrlEncode(file, encoding)));*
      

      【讨论】:

        【解决方案4】:

        要为具有自定义文件名的任何文件类型修复此问题,请删除(或类似标题)

        Response.AppendHeader("Content-Disposition", "inline;");
        

        并添加

        string fileName = "myfile.xlsx"
        return File(fileStream, System.Web.MimeMapping.GetMimeMapping(Path.GetFileName(filePath)), fileName);
        

        您也可以在第一个参数中使用文件路径而不是流

        【讨论】:

          【解决方案5】:

          我使用了@roryok 的评论,将文件名用引号括起来:

          Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + "\"")
          

          @a coder's answer of using single quotes 在 IE 中没有按预期工作。下载的文件名称中仍带有单引号。

          【讨论】:

          • 这个解决方案对我有用。我刚刚添加了转义的双引号,问题就自行解决了。谢谢!
          • 嘿,我试过了,但是下载时文件名带有下划线前缀和后缀。 (Content-Disposition: attachment; filename="ab,cd.pptx") 实际名称 -> ab,cd.pptx... NameOnDownload -> ab,cd.pptx ... 我能做什么为这个问题做些什么?
          【解决方案6】:

          今天遇到了这个问题。根据 roryok 和其他人,解决方案是将文件名放在引号中。

          以前,Chrome 失败:

          header("Content-Disposition: attachment; filename=$file");
          

          当前,Chrome OK:

          header("Content-Disposition: attachment; filename='$file'");
          

          注意$file 周围的引号。

          【讨论】:

          • 对我来说,在 IE 中,单引号导致文件下载时文件名周围带有单引号:'Blah, Inc. 847.pdf'
          【解决方案7】:

          如果您不需要指定文件名,上述解决方案很好,但我们希望保留为用户指定的文件名默认值。

          我们的解决方案最终是文件名本身,因为它包含一些逗号。我用“”替换了逗号,现在文件按预期在 Chrome 中提供了文档。

          FileName = FileName.Replace(",", "")
          
          Response.ContentType = "application/pdf"
          Response.AddHeader("content-disposition", "attachment; filename=" & FileName)    
          Response.BinaryWrite(myPDF)
          

          【讨论】:

          • 您还可以在文件名周围添加逗号。
          • DocRaptor 和 PHP header() 的相同问题和解决方案。谢谢。
          • 这里有同样的问题,但只能通过将文件名放在引号中来解决:Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + "\"")
          • @Nik 你应该用引号解决方案发布一个答案,因为它与这个完全不同并且效果很好。我会赞成这个答案。
          • 今天遇到了 Azure Blob 存储和 Content-Disposition 标头的问题。感谢您的修复 - 效果很好!
          猜你喜欢
          • 2015-04-09
          • 2012-11-14
          • 1970-01-01
          • 2012-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-24
          相关资源
          最近更新 更多