C# 如何在新窗口中打开图片或pdf文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16579410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open image or pdf file in a new window?
提问by Sam Simon
i have a gridview it contains file names and path of files (image and pdf format files) in that i used template field under that i put 1 image buttoon. on clicking of that image button i.e view button i want to open selected file in a new window.
我有一个 gridview,它包含文件名和文件路径(图像和 pdf 格式文件),因为我使用了模板字段,我在其中放置了 1 个图像按钮。单击该图像按钮,即查看按钮,我想在新窗口中打开选定的文件。
here is my code :
这是我的代码:
protected void GVViewFile_SelectedIndexChanged(object sender, EventArgs e)
{
int id = GVViewFile.SelectedIndex;
string path = GVViewFile.Rows[id].Cells[2].Text.ToString();
Response.Redirect("D:\UploadedAttachment\AT\MRD\AT0520130008_15-05-13-03-57-12.pdf");
Response.Write("<script>");
Response.Write("window.open('" + path + "','_blank', ' fullscreen=yes')");
//Response.Write("window.open(" + path + ",'_blank')");
Response.Write("</script>");
}
but i could not open in new window. my path returns same value as inside response.write(). whe i use just response.write("images/UserDetails.pdf");as example it will show pdf page..but full path is not taking. also it shows '\'is wrong in response.write();so how to use actual full path to display image or pdf in new window..please help me.even that window.open is giving error.i cannot write full path in window.open since i am getting selected path from gridview.help please....
但我无法在新窗口中打开。我的路径返回与内部 response.write() 相同的值。当我仅response.write("images/UserDetails.pdf");作为示例使用时,它将显示 pdf 页面..但未采用完整路径。它也显示'\'错误,response.write();所以如何使用实际完整路径在新窗口中显示图像或 pdf ..请帮助我。即使那个 window.open 给出错误。我无法在 window.open 中写入完整路径,因为我被选中了来自gridview.help的路径请....
my gridview code :
我的网格视图代码:
<asp:GridView ID="GVViewFile" runat="server" AutoGenerateColumns="False"
DataSourceID="DSforgridview" onselectedindexchanged="GVViewFile_SelectedIndexChanged"
HeaderStyle-BackColor="#CC6600" HeaderStyle-ForeColor="White"
PagerStyle-BackColor="#CC6600" PagerStyle-ForeColor="White" CellPadding="3"
CellSpacing="3" PagerStyle-Width="4" PagerStyle-Height="4"
BorderColor="#FF6600" BorderStyle="Solid">
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:ImageButton ID="btnView" runat="server"
CausesValidation="False" CommandName="Select"
ImageUrl="~/Images/view.gif" ToolTip="View File" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FileType" HeaderText="FileType"
SortExpression="FileType" />
<asp:BoundField DataField="FileLocationPath" HeaderText="FileLocationPath"
SortExpression="FileLocationPath" />
</Columns>
<HeaderStyle BackColor="#CC6600" ForeColor="White"></HeaderStyle>
<EmptyDataTemplate>No Records Found.</EmptyDataTemplate>
</asp:GridView>
采纳答案by karthi
//In Default2.aspx
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "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);
}
}
回答by Amit
It will work with relative path only. Why in the first place you need path? Also user Registerstartupscript for script binding to page.
它仅适用于相对路径。为什么首先需要路径?用户还可以使用 Registerstartupscript 将脚本绑定到页面。
回答by Fanda
In html response you are operating with url paths. So path to open should be valid url (absolute or relative to application), or link to file: "file://path/to/file", which open some directory browser in computer.
在 html 响应中,您使用的是 url 路径。所以打开的路径应该是有效的url(绝对或相对于应用程序),或者链接到文件:“file://path/to/file”,它在计算机中打开某个目录浏览器。
You can just put HyperLink control with NavigateUrl with target="_blank" or some javascript. Link to absolute server path will not work.
您可以使用带有 target="_blank" 或一些 javascript 的 NavigateUrl 放置 HyperLink 控件。链接到绝对服务器路径将不起作用。
回答by karthi
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "pdf/aaaa.PDF"));
回答by drhanlau
when you bind your FileLocationPath, try to bind it so that your filename
当您绑定 FileLocationPath 时,尝试绑定它以便您的文件名
D:\UploadedAttachment\AT\MRD\AT0520130008_15-05-13-03-57-12.pdf
become
变得
file:///D:/UploadedAttachment/AT/MRD/AT0520130008_15-05-13-03-57-12.pdf
回答by Ramdas
The abort() function is probably your best bet. It's part of the C standard library, and is defined as "causing abnormal program termination" (e.g, a fatal error or crash).
abort() 函数可能是您最好的选择。它是 C 标准库的一部分,被定义为“导致程序异常终止”(例如,致命错误或崩溃)。

