jeudi 5 mars 2015

How to download a PDF file from a Gridview using the path saved in the Database


Vote count:

0




I have written an application to generate a PDF using iTextSharp and save it in a particular local folder. On saving the PDF, I have coded the path to be written to the Database as well. On another interface I have a Gridview where I have specific data rows. I have given a linkbutton to appear on each data row. What I need to do is, when a user clicks on a linkbutton, the linkbutton should read the particular filepath where the original PDF was saved and download the PDF to the default "Downloads" folder in Windows.


Here is my code :


Gridview :



<asp:GridView ID="Gridview1" runat="server" ShowHeaderWhenEmpty="true"
EmptyDataText="Empty Records" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="true" Font-Size="Small" Width="95%">
<AlternatingRowStyle BackColor="White" />

<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("id") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField HeaderText="ID" DataField="id" DataFormatString="Document/{0}" />
<asp:BoundField HeaderText="Company Name" DataField="ComName" />
<asp:BoundField HeaderText="Country Incorperated" DataField="Country" />
<asp:BoundField HeaderText="Date Created" DataField="Date" />
</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>


Codebehind :



protected void DownloadFile(object sender, EventArgs e)
{
Processor process = new Processor();

string id = Gridview1.SelectedRow.Cells[1].Text;
string[] lines = Regex.Split(id, "/");
string ID = lines[4];

string filePath = process.getPath(Int32.Parse(ID));
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}


I came up with this code from this site : http://ift.tt/1EkJnFa


I also used a breakpoint on the line string id = Gridview1.SelectedRow.Cells[1].Text; and it returned the value as null. Please help.



asked 50 secs ago







How to download a PDF file from a Gridview using the path saved in the Database

Aucun commentaire:

Enregistrer un commentaire