内容处理:ASP.Net中的PDF文件内联不起作用

时间:2018-05-18 08:31:10

标签: c# asp.net pdf mozilla content-disposition

我正在尝试在创建此文件后立即将PDF文件返回到带有标题Content-Dispostion:inline的浏览器。 浏览器的浏览者有问题要打开它。

文件未损坏。如果我放入浏览器,查看器会正确显示文件。 但我希望创建文件并在一次请求时检查文件的用户权限。

以下是我的标题和回复设置:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline");
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("filename", $"objednavka-{OrderId}.pdf");
HttpContext.Current.Response.BinaryWrite(File.ReadAllBytes(filePath));
HttpContext.Current.Response.End();

Firefox在文件 viewer.js 时出错,消息未定义,第1453行。

Firefox的调试器中的网络响应 200 OK

但浏览器中的消息是连接丢失(中断)。

1 个答案:

答案 0 :(得分:1)

两个建议都有帮助。感谢你们。 这是看起来正确的答案:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", $"inline; filename=\"objednavka-{ OrderId}.pdf\"");
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("content-length", File.ReadAllBytes(filePath).Length.ToString());
HttpContext.Current.Response.BinaryWrite(File.ReadAllBytes(filePath));
HttpContext.Current.Response.End();