'c# PDF downloader does not start the download
I have some code that I have researched to hopefully create a PDF document of the data held in an asp repeater table. The repeater and eveything else on the page looks good and works as intended, downloading information from SQL and displaying it to the user.
My problem is that the software owner has requested a function to create a PDF document to use for auditing evidence. I have a button that when clicked runs an onClick event handler to perform the following
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
pnlhistory.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
in the researched examples and demos I've seen this starts a downlod of the PDF which can then be viewed. However when I step through the code it all seems okay (no exceptions), then after responce.end(); it returns to the webbrowser where nothing happens.
Have I overlooked an important line of code to tell the browser to start downloading?
the repeater is inside a div which is hidden until a show history checkbox in another table is clicked by the user. The control referenced in the c# is the asp panel seen below.
<asp:Panel ID="pnlhistory" runat="server">
<div class="datagrid" id="divHis" runat="server" visible="false">
<div style="font-family: Verdana; background:#F8F8FF; padding:0px;" >
<asp:Repeater ID="rptHis" runat="server">
<HeaderTemplate>
<table id="tbl" style="width: 100%; text-align: center;">
<tr class="headerColour">
<th>Serial Number</th>
<th>Task ID</th>
<th>Service Request ID</th>
<th>Task Type</th>
<th>Date Raised </th>
<th>Raised By</th>
<th>Current Request Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="<%# Container.ItemIndex % 2 == 0 ? "oddRow" : "evenRow" %>">
<td align="center" style="padding: 10px; font-size: 13px;"><asp:Label ID="lbl1" runat="server" Text='<%# Eval("data") %>' /></td>
<td align="center" style="padding: 10px; font-size: 13px;"><asp:Label ID="lbl2" runat="server" Text='<%# Eval("data") %>' /></td>
<td align="center" style="padding: 10px; font-size: 13px;"><asp:HyperLink ID="hyp1" runat="server" Text='<%# Eval("data") %>' ForeColor="Blue" Target="_blank" NavigateUrl='<%# "website.aspx?id=" + Eval("data") %>'></asp:HyperLink></td>
<td align="center" style="padding: 10px; font-size: 13px;"><asp:Label ID="lbl23" runat="server" Text='<%# Eval("data") %>' /></td>
<td align="center" style="padding: 10px; font-size: 13px;"><asp:Label ID="Label3" runat="server" Text='<%# method2(Eval("data")) %>' /></td>
<td align="center" style="padding: 10px; font-size: 13px;"><asp:Label ID="Label1" runat="server" Text='<%# Eval("data") %>' /></td>
<td align="center" style="padding: 10px; font-size: 13px;"><asp:Label ID="Label2" runat="server" Text='<%# Eval("data") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</asp:Panel>
I'm not a complete c# novice but I am largly self taught and would welcome any advice that helps me get a working PDF!
thank you all in advance
Solution 1:[1]
try to change response contentype in Response.ContentType = "application/octet-stream"; and Response.Write in Response.WriteFile(file.FullName);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | realexweb |
