HTTPS 内容处理附件

HTTPS content-disposition attachment

症状与 C# BinaryWrite over SSL 非常相似,但我的代码已经符合所有建议的解决方案。在撰写本文时,此行为在 Edge、IE11 和 Chrome 上很明显。通过 HTTPS 交付时,在 HTTP 上运行良好的代码崩溃了。

private void RenderPdfToResponse(bool asDownload, byte[] documentBytes)
{
  Response.BufferOutput = true;
  Response.ClearContent();
  Response.ClearHeaders();
  Response.AddHeader("Cache-control", "no-store");
  Response.ContentType = "application/pdf";
  Response.AddHeader("Content-Length", documentBytes.Length.ToString());
  if (asDownload)
    Response.AddHeader("Content-Disposition", 
      string.Format("attachment; filename=export{0:yyyyMMddHHmmss}.pdf", DateTime.UtcNow));
  Response.BinaryWrite(documentBytes);
  Response.Flush();
  HttpContext.Current.ApplicationInstance.CompleteRequest();
}

问题出在浏览器上。应保存到文件中的已交付二进制内容反而在浏览器中呈现 window。使用调试工具进行拦截并保存到文件中可以验证传送的字节是否构成有效的 PDF。

鉴于所引用问题的所有建议都已经生效,并且问题出在浏览器行为上,还有哪些选择?

咨询https://www.rfc-editor.org/rfc/rfc6266我们发现文件名部分是可选的。实验指定

Response.AddHeader("Content-Disposition", "attachment");

导致所有主流浏览器弹出一个名称为 export-pdf.pdf 的另存为对话框,该名称显然源自 ASPX 文件的名称和 mime 类型。

此时我被要求发货。我希望有人觉得这有帮助。