从DataGrid在默认查看器中打开PDF

时间:2013-03-19 17:09:37

标签: c# asp.net pdf

我有一个显示名称和网格的网格链接到文件夹中的所有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; }
    }

1 个答案:

答案 0 :(得分: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 ...

并在Page load event的{​​{1}}上写下以下代码..

C#:

Download.aspx