如何在新窗口中打开.pdf? [英] How to open a .pdf in a new window?
本文介绍了如何在新窗口中打开.pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have the following code
string path = Server.MapPath("\\Reports\\" + DDEP.SelectedValue + ".pdf");
ClientScript.RegisterStartupScript(this.GetType(), "open", "window.open(''"+path+"'',''_blank'' );", true);
I am trying to open the .pdf in a new window
but its opening a blank window.
thanks for the help
我尝试过的事情:
What I have tried:
string path = Server.MapPath("\\Reports\\" + DDEP.SelectedValue + ".pdf");
ClientScript.RegisterStartupScript(this.GetType(), "open", "window.open(''"+path+"'',''_blank'' );", true);
/*
StringBuilder sb = new StringBuilder();
sb.Append("<script type = ''text/javascript''>");
sb.Append("window.open(''");
sb.Append(Server.MapPath("\\Reports\\" + DDEP.SelectedValue + ".pdf"));
sb.Append("'');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(),
"script", sb.ToString());
*/
推荐答案
我问了Google Gods您的问题: ^ ],原为给出了数千个有用的答案...
这是给出的第一个: javascript-在新的浏览器全窗口中打开PDF-堆栈溢出 [
I asked the Google Gods your question: javascript open pdf in new window[^] and was given thousands of useful answers...
Here is the first one given: javascript - Open PDF in new browser full window - Stack Overflow[^]
<a href="#" onclick="window.open('MyPDF.pdf', '_blank', 'fullscreen=yes'); return false;">MyPDF</a>
按照您的方式,将类似于:
Which, done your way, would be something like:
ClientScript.RegisterStartupScript(this.GetType(), "open", "window.open('"+path+"','_blank', 'fullscreen=yes');", true);
不用担心Google Gods,它们非常友善并给予...
Don''t be afraid of the Google Gods, they are very kind and giving...
Server.MapPath
返回服务器上文件的本地路径.
用户无权访问服务器的本地文件系统.它只能访问您网站中定义的路径.
您传递给window.open
的路径可以是以下之一:
- 相对于当前页面的路径:
../reports/file.pdf
- 相对于当前站点的路径:
/reports/file.pdf
- 完整的URL:
https://www.yoursite.com/reports/file.pdf
Server.MapPath
returns the local path of the file on the server.
The user doesn''t have access to the server''s local file system. It only has access to the paths defined within your website.
The path you pass towindow.open
can be one of the following:
- A path relative to the current page:
../reports/file.pdf
- A path relative to the current site:
/reports/file.pdf
- A full URL:
https://www.yoursite.com/reports/file.pdf
string path = ResolveClientUrl("~/Reports/" + DDEP.SelectedValue + ".pdf");
string encodedPath = System.Web.HttpUtility.JavaScriptStringEncode(path, true);
ClientScript.RegisterStartupScript(this.GetType(), "open", "window.open(" + encodedPath + ", ''_blank'');", true);
注意:您需要调用JavaScriptStringEncode
以确保正确编码了任何特殊字符.通过将true
作为第二个参数,可以确保字符串正确加引号,因此您不需要在其周围包括''
字符.
NB: You need to call JavaScriptStringEncode
to ensure that any special characters are properly encoded. By passing true
as the second parameter, you can ensure that the string is properly quoted, so you don''t need to include the ''
characters around it.
这篇关于如何在新窗口中打开.pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文