Content-Disposition: inline for PDF file in ASP.Net not working

无人久伴 提交于 2019-12-11 05:08:35

问题


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

Response in Network in debugger of Firefox is 200 OK.

But message in browser is connection lost (interupted).


回答1:


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


来源:https://stackoverflow.com/questions/50407025/content-disposition-inline-for-pdf-file-in-asp-net-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!