ASP.NET 4: HttpResponse open in NEW Browser?

耗尽温柔 提交于 2019-12-31 07:53:51

问题


I am using a PDF generater that utlizes HttpResponse. Is there a way (perhaps passing a header tag) to open the PDF in a NEW windows instead of the same one? I don't want the user to be directed away from the website...

Here's the code I'm using:

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;        response.Clear();
response.AddHeader("Content-Type", "application/pdf");
response.AddHeader("Content-Disposition",
    "inline; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();

回答1:


You cannot do that on server side if you have not generated the link yourself. But if you have, then as Robert said, provide a target with the <a> link.

There is a server-side alternative and that is to set the content type to application/octect-stream so that the file is download and user will be able to open it with the application of choice outside browser. See here for more.

You also need to use content disposition header to provide the file name so that client can know what file type is it after it has been downloaded as binary.

Content-Disposition: attachment; filename=my.pdf;



回答2:


You presumably have a link on your site that directs the user to the URL where this PDF generation is done. In that link, add target="_blank" to the <a> tag.




回答3:


If what you're really asking is how to make the save dialog to show instead of having the PDF loaded into your browser, then you need to modify your header. You are setting Content-Disposition to 'inline', change that to 'attachment'

http://support.microsoft.com/kb/260519



来源:https://stackoverflow.com/questions/4823793/asp-net-4-httpresponse-open-in-new-browser

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