1

我有一个网格,显示文件夹中所有 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; }
    }
4

1 回答 1

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..Download.aspx

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(); 
        }
于 2013-03-19T18:37:55.020 回答