如何在 Gridview 中下载给定文件路径生成的 PDF 文件?
How do I download the PDF file generated given the file path in a Gridview?
我想编写代码以允许用户在 Gridview 中给定文件路径时通过单击 Gridview 中的 "Download" 链接按钮来下载 PDF。
这是我的代码:
aspx
<asp:GridView ID="Gridview1" runat="server" ShowHeaderWhenEmpty="true"
EmptyDataText="No Records Found" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="true" Font-Size="Small" Width="95%" onselectedindexchanged="Gridview1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="ID" DataField="id" DataFormatString="BSc/{0}" />
<asp:BoundField HeaderText="Company Name" DataField="ComName" />
<asp:BoundField HeaderText="Country Incorperated" DataField="Country" />
<asp:BoundField HeaderText="Date Created" DataField="Date" />
<asp:BoundField HeaderText="Path" DataField="Path" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Path") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
aspx.cs
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
我从这个站点查找了下载代码:http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx
我也得到了我应该得到的文件路径,但此后没有任何反应。也没有报错。
请帮忙。提前致谢。
Response.WriteFile
是 ASP.NET 1.1 中使用的旧方法。
请尝试使用此代码进行下载。
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
// Add the file size into the response header
Response.AddHeader("Content-Length", filePath.Length.ToString());
// Set the ContentType
Response.ContentType = "application/pdf";
// Write the file into the response (TransmitFile is for ASP.NET 2.0 and above. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(filePath);
Response.Flush();
// End the response
Response.End();
此外,在更新面板中包含网格可能会导致尝试从网格本身内的控件下载时出现问题,因为下载需要完整 post 返回。在这种情况下,请考虑使用 PostBackTrigger.
我想编写代码以允许用户在 Gridview 中给定文件路径时通过单击 Gridview 中的 "Download" 链接按钮来下载 PDF。
这是我的代码:
aspx
<asp:GridView ID="Gridview1" runat="server" ShowHeaderWhenEmpty="true"
EmptyDataText="No Records Found" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="true" Font-Size="Small" Width="95%" onselectedindexchanged="Gridview1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="ID" DataField="id" DataFormatString="BSc/{0}" />
<asp:BoundField HeaderText="Company Name" DataField="ComName" />
<asp:BoundField HeaderText="Country Incorperated" DataField="Country" />
<asp:BoundField HeaderText="Date Created" DataField="Date" />
<asp:BoundField HeaderText="Path" DataField="Path" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Path") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
aspx.cs
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
我从这个站点查找了下载代码:http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx
我也得到了我应该得到的文件路径,但此后没有任何反应。也没有报错。
请帮忙。提前致谢。
Response.WriteFile
是 ASP.NET 1.1 中使用的旧方法。
请尝试使用此代码进行下载。
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
// Add the file size into the response header
Response.AddHeader("Content-Length", filePath.Length.ToString());
// Set the ContentType
Response.ContentType = "application/pdf";
// Write the file into the response (TransmitFile is for ASP.NET 2.0 and above. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(filePath);
Response.Flush();
// End the response
Response.End();
此外,在更新面板中包含网格可能会导致尝试从网格本身内的控件下载时出现问题,因为下载需要完整 post 返回。在这种情况下,请考虑使用 PostBackTrigger.