[英]How to open image or pdf file in a new window?
我有一個gridview它包含文件名和文件(圖像和pdf格式文件)的路徑,在其中我使用了模板字段,在其中放置了1個圖像按鈕。 在單擊該圖像按鈕即查看按鈕時,我想在新窗口中打開所選文件。
這是我的代碼:
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>");
}
但我無法在新窗口中打開。 我的路徑返回與response.write()內部相同的值。 當我只使用response.write("images/UserDetails.pdf");
作為示例,它將顯示pdf頁面。.但是沒有完整的路徑。 還表明response.write();
中的'\\'
是錯誤的response.write();
所以如何使用實際的完整路徑在新窗口中顯示圖像或pdf ..請幫助我。即使window.open出現錯誤。我無法在window.open中寫入完整路徑,因為我是從gridview.help獲取選擇的路徑....
我的GridView代碼:
<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);
}
}
它僅適用於相對路徑。 為什么首先需要路徑? 另外,用戶Registerstartupscript用於將腳本綁定到頁面。
在html響應中,您正在使用url路徑。 因此,打開的路徑應該是有效的url(絕對或相對於應用程序),或鏈接到文件:“ file:// path / to / file”,這會在計算機中打開某些目錄瀏覽器。
您可以將HyperLink控件與帶有target =“ _ blank”或某些javascript的NavigateUrl一起使用。 鏈接到絕對服務器路徑將不起作用。
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "pdf/aaaa.PDF"));
當您綁定FileLocationPath時,請嘗試將其綁定,以便您的文件名
D:\UploadedAttachment\AT\MRD\AT0520130008_15-05-13-03-57-12.pdf
成為
file:///D:/UploadedAttachment/AT/MRD/AT0520130008_15-05-13-03-57-12.pdf
abort()函數可能是最好的選擇。 它是C標准庫的一部分,被定義為“導致程序異常終止”(例如,致命錯誤或崩潰)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.