2013-05-16 54 views
2

我有一個gridview它包含文件名和路徑的文件(圖像和pdf格式文件),我使用了模板字段下,我把1圖像buttoon。點擊該圖像按鈕,即查看按鈕,我想在新窗口中打開選定的文件。如何在新窗口中打開圖片或pdf文件?

這裏是我的代碼:

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();所以如何使用實際的完整路徑來顯示圖像或pdf在新窗口..請幫助我.even那個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> 
+0

@cherhan我粘貼,但概率不是在gridview中,概率是我不能打開新的路徑(即像D:\ UploadedAttachment \ AT \ MRD \ AT0520130008_15-05-13-03-57-12.pdf)在新的「 –

+0

」〜/ UploadedAttachment \ AT \ MRD \ AT0520130008_15-05-13-03-57-12.pdf「試試這個... –

+0

您使用的瀏覽器是什麼? – cherhan

回答

5
//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); 
     } 
    } 
+0

你只是試試這個代碼 – karthi

+0

嘿謝謝..它的作品:-) –

+0

真棒代碼!我正在使用選項2,但是如何在新窗口中打開? –

0

它只能與相對路徑工作。 爲什麼首先你需要路徑? 也用戶Registerstartupscript腳本綁定到頁面。

+0

我會得到路徑。所以我只能使用它。路徑是完整路徑。 wat做什麼? –

0

在html響應中,您正在使用url路徑進行操作。因此,打開的路徑應該是有效的url(絕對或相對於應用程序),或者鏈接到文件:「file:// path/to/file」,它打開計算機中的某個目錄瀏覽器。

你可以把HyperLink控件與NavigateUrl與目標=「_空白」或一些JavaScript。鏈接到絕對服務器路徑將不起作用。

+0

那麼我的解決方案是什麼?你能提出一些建議嗎? –

0
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "pdf/aaaa.PDF")); 
+0

嘿如何在那裏使用字符串'路徑'? –

+0

pdf/aaaa.PDF是你不放的pdf文件的路徑〜/或/之前pdf/aaaa.PDF – karthi

+0

我想你沒有得到我的觀點。我有顯示圖像,它的路徑是在D:\ UploadedAttachment \ AT \ MRD \ AT0520130008_15-05-13-03-57-12.pdf 以下,我從字符串路徑獲取。 –

0

當你綁定你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 
+0

顯示錯誤:網頁無法顯示 –

+0

alrite,你可以截圖嗎? – cherhan

0

的中止()函數可能是你最好的選擇。它是C標準庫的一部分,被定義爲「導致異常程序終止」(例如,致命錯誤或崩潰)。

相關問題