如何在新窗口中打开图像或PDF文件? [英] How to open image or pdf file in a new window?
问题描述
我把它包含在文件名和文件(图像和PDF格式文件)的路径我用下,我把1图像buttoon模板领域的GridView。在该图像按钮即视图按钮点击我想在新窗口中打开选定的文件。
这是我的code:
保护无效GVViewFile_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
INT ID = GVViewFile.SelectedIndex;
字符串路径= GVViewFile.Rows [ID] .Cells [2] .Text.ToString(); 的Response.Redirect(D:\\ UploadedAttachment \\ AT \\ MRD \\ AT0520130008_15-05-13-03-57-12.pdf); 的Response.Write(<脚本>中);
的Response.Write(window.open('+路径+','_空白','全屏= YES'));
//Response.Write(window.open(+路径+,_空白'));
回复于(&下; /脚本>中);}
但我无法在新窗口中打开。我的路径返回相同的值里面的Response.Write()。磨片我只使用的Response.Write(图像/ UserDetails.pdf);
为例,它会显示PDF page..but完整路径而不是索取。同时它显示了'\\'
是的Response.Write(错误);
那么如何使用实际的完整路径显示图像或PDF在新window..please帮助me.even了的window.open是给error.i不能写在window.open完整路径,因为我正在从选择路径gridview.help请....
我的GridView code:
< ASP:GridView控件ID =GVViewFile=服务器的AutoGenerateColumns =FALSE
的DataSourceID =DSforgridviewonselectedindexchanged =GVViewFile_SelectedIndexChanged
HeaderStyle-背景色=#CC6600HeaderStyle-前景色=白
PagerStyle-背景色=#CC6600PagerStyle-前景色=白的cellpadding =3
CELLSPACING =3PagerStyle宽度=4PagerStyle-高度=4
BORDERCOLOR =#FF6600边框=固体>
<柱体和GT;
< ASP:的TemplateField ShowHeader =false的>
<&ItemTemplate中GT;
< ASP:ImageButton的ID =btnView=服务器
的CausesValidation =FALSE的CommandName =选择
的ImageUrl =〜/图片/ view.gif工具提示=查看文件/>
< / ItemTemplate中>
< / ASP:的TemplateField> < ASP:BoundField的数据字段=文件类型的HeaderText =文件类型
SORTEX pression =文件类型/>
< ASP:BoundField的数据字段=FileLocationPath的HeaderText =FileLocationPath
SORTEX pression =FileLocationPath/>
< /专栏>
< HeaderStyle背景色=#CC6600前景色=白>< / HeaderStyle>
< EmptyDataTemplate>没有找到记录< / EmptyDataTemplate>
< / ASP:GridView的>
//在Default2.aspx
保护无效LinkButton1_Click(对象发件人,EventArgs的发送)
{
回复于(的String.Format(<脚本> window.open('{0}','_空白');< / SCRIPT>中,Default3.aspx));
}// ------------
//在Default3.aspx保护无效的Page_Load(对象发件人,EventArgs的发送)
{
字符串路径=使用Server.Mappath(〜\\\\ E:\\\\ \\\\卡菲基恩\\\\ venky PDF \\\\ aaaa.PDF);
Web客户端的客户端=新的WebClient();
字节[]缓冲= client.DownloadData(路径);
如果(缓冲!= NULL)
{
Response.ContentType =应用程序/ PDF
Response.AddHeader(内容长度,buffer.Length.ToString());
Response.BinaryWrite(缓冲液);
}
}
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.
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....
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>
//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);
}
}
这篇关于如何在新窗口中打开图像或PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!