如何在新窗口中打开图像或pdf文件?
我有一个gridview,它包含文件名和文件路径(图像和pdf格式文件)在我使用的模板字段下,我把1图像buttoon。 单击该图像按钮即查看按钮我想在新窗口中打开所选文件。
这是我的代码:
protected void GVViewFile_SelectedIndexChanged(object sender, EventArgs e) { int id = GVViewFile.SelectedIndex; string path = GVViewFile.Rows[id].Cells[2].Text.ToString(); Response.Redirect("D:UploadedAttachmentATMRDAT0520130008_15-05-13-03-57-12.pdf"); Response.Write(""); Response.Write("window.open('" + path + "','_blank', ' fullscreen=yes')"); //Response.Write("window.open(" + path + ",'_blank')"); Response.Write(""); }
但我无法在新窗口中打开。 我的路径返回与response.write()内部相同的值。 我只使用response.write("images/UserDetails.pdf");
例如,它将显示pdf页面..但完整路径没有采取。 它还表明''
在response.write();
是错误的response.write();
所以如何使用实际的完整路径在新窗口中显示图像或pdf ..请帮助我….即window.open给出错误。我无法在window.open中写入完整路径,因为我从gridview.help获取选定的路径请….
我的gridview代码:
No Records Found.
//In Default2.aspx protected void LinkButton1_Click(object sender, EventArgs e) { Response.Write(string.Format("", "Default3.aspx")); } //------------ //In Default3.aspx protected void Page_Load(object sender, EventArgs e) { string path = Server.MapPath("~\E:\karthikeyan\venky\pdf\aaaa.PDF"); WebClient client = new WebClient(); Byte[] buffer = client.DownloadData(path); if (buffer != null) { Response.ContentType = "application/pdf"; Response.AddHeader("content-length", buffer.Length.ToString()); Response.BinaryWrite(buffer); } }
它只适用于相对路径。 为什么首先你需要路径? 还有用户Registerstartupscript用于脚本绑定到页面。
在html响应中,您正在使用url路径。 因此,打开的路径应该是有效的URL(绝对或相对于应用程序),或链接到文件:“file:// path / to / file”,它在计算机中打开一些目录浏览器。
您可以使用带有target =“_ blank”或某些javascript的NavigateUrl放置HyperLink控件。 链接到绝对服务器路径将不起作用。
Response.Write(string.Format("", "pdf/aaaa.PDF"));
绑定FileLocationPath时,尝试绑定它以使您的文件名
D:UploadedAttachmentATMRDAT0520130008_15-05-13-03-57-12.pdf
成为
file:///D:/UploadedAttachment/AT/MRD/AT0520130008_15-05-13-03-57-12.pdf
abort()函数可能是你最好的选择。 它是C标准库的一部分,定义为“导致程序exception终止”(例如,致命错误或崩溃)。
上述就是C#学习教程:如何在新窗口中打开图像或pdf文件?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1022448.html