内容处置:ASP.Net中的PDF文件内联无法正常工作 [英] Content-Disposition: inline for PDF file in ASP.Net not working

查看:314
本文介绍了内容处置:ASP.Net中的PDF文件内联无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建此文件后,我正尝试将PDF文件返回到标题为Content-Dispostion:inline的浏览器. 浏览器的浏览器无法打开它.

I am trying to return PDF file to browser with header Content-Dispostion:inline right after I am creating this file. Viewers of browser have problem to open it.

文件未损坏.如果放入浏览器,查看器会正确显示文件. 但是我想创建文件并一次请求就检查用户权限.

File is not corrupted. If I put into browser, viewer shows file correctly. But I want create file and check users rights for file at one request.

这是我的标题和响应设置:

Here is my headers and response set up:

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 have error at file viewer.js, message undefined, line 1453.

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

Response in Network in debugger of Firefox is 200 OK.

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

But message in browser is connection lost (interupted).

推荐答案

这两个建议都可以提供帮助.感谢大伙们. 这是正确答案的样子:

both advices help. Thank you guys. this is how looks correct answer:

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();

这篇关于内容处置:ASP.Net中的PDF文件内联无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆