2013-03-19 31 views
1

我有一個網格,顯示名稱&鏈接到文件夾中的所有pdf文件。該鏈接在瀏覽器中打開該文件。從DataGrid在默認查看器中打開PDF

這並不理想,因爲瀏覽器會緩存文件,因此如果文檔被移動或更改,舊的緩存版本仍然可以訪問。

是否有可能讓鏈接在默認安裝的pdf查看器中打開文件並避免完全緩存?

這裏是我的網代碼:

<form runat="server"> 
Search for a file: <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox> 
    <asp:Button ID="btnShow" 
     runat="server" Text="Display" onclick="btnShow_Click" /> 
</form> 


<asp:DataGrid runat="server" id="FileList" Font-Name="Verdana" CellPadding="5" 
AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee" 
HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White" 
HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True"> 
<Columns> 
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" 
     HeaderText="File Name" target="_blank"/> 
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time" 
    ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" /> 
</Columns> 
</asp:DataGrid> 

CS:

protected void btnShow_Click(object sender, EventArgs e) 
    { 
    ShowData(); 
    } 

    public void ShowData() 
    { 
     DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("")); 

     FileInfo[] info = dirInfo.GetFiles("*.pdf");   //Get FileInfo and Save it a FileInfo[] Array 

     List<Getfiles> _items = new List<Getfiles>();   // Define a List with Two coloums 

     foreach (FileInfo file in info) //Loop the FileInfo[] Array 
      _items.Add(new Getfiles { Name = file.Name, LastWriteTime = file.LastWriteTime.ToString("MM/dd/yyyy") }); // Save the Name and LastwriteTime to List 


     var tlistFiltered1 = _items.Where(item => item.Name.Contains(txtFilter.Text)); // Find the file that Contains Specific word in its File Name 

     FileList.DataSource = tlistFiltered1; //Assign the DataSource to DataGrid 
     FileList.DataBind(); 

    } 

    public class Getfiles 
    { 
     public string Name { get; set; } 
     public string LastWriteTime { get; set; } 
    } 
+0

我的默認PDF查看器是谷歌瀏覽器...所以對我來說它可能無法正常工作。也許你可以以某種方式禁用Web服務器級別的PDF文件緩存? – nvoigt 2013-03-19 18:35:11

+0

@nvoigt可以這樣做嗎?即使您進行了服務器級別的更改,我的瀏覽器仍不會緩存嗎?我也使用Chrome。 – Jack 2013-03-19 18:47:58

回答

0

更好您提示一個對話框,用戶,並要求他下載的文件,那麼它會避免瀏覽器緩存.. 。

嘗試像下面...

HTML:

Change the DataGrid HyperLinkColumn象下面這樣:

<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" 
     HeaderText="File Name" target="_blank" DataNavigateUrlFormatString="Download.aspx?FileName={0}" /> 

然後Add a New .aspx page到您的網站,並把它命名爲Download.aspx ...

,並在Download.aspxPage load event寫如下的代碼..

C#:

 protected void Page_Load(object sender, EventArgs e) 
     { 
      string FName = Server.MapPath(@"Files\" + Request.QueryString["FileName"].ToString()); 
      Response.Clear(); 
      Response.ContentType = "application/pdf"; 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + FName); 
      Response.TransmitFile(FName); 
      Response.End(); 
     } 
+0

這樣做的目的是避免緩存,但它會在每個下載它的用戶的計算機上創建一個文檔副本,這並不理想。它仍然是一個選擇。我是否需要尋找一個開源的pdf網頁瀏覽器並找到一種將文檔傳遞給它的方法? – Jack 2013-03-19 18:52:25

+0

感謝您的建議。 – Jack 2013-03-19 18:53:50

+0

您是否認爲如果PDF在iFrame中會被緩存? – Jack 2013-03-20 18:47:12

相關問題