在DataGrid的默认查看器中打开PDF
本文关键字:PDF DataGrid 默认 | 更新日期: 2023-09-27 18:04:38
我有一个显示名称&链接到一个文件夹中的所有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; }
}
你最好给用户提示一个对话框,让他下载文件,这样就可以避免在浏览器中缓存…
尝试如下…
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.aspx
的Page load event
上写下面的代码…
:
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();
}