Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Client download pdf file using

Response.BinaryWrite ASP.NET c#
Asked 4 years, 8 months ago
Viewed 2k times
0

I'm trying to add a feature to download a pdf file. I'm using ironpdf to generate the pdf file
and I want the user to click and download it.

Here is my handler.

try
{
// Render any HTML fragment or document to HTML
var Renderer = new IronPdf.HtmlToPdf();
var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
var data = PDF.MetaData;
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition",
"attachment;filename=Cotacao.pdf");
context.Response.BinaryWrite(PDF.BinaryData);
context.Response.Flush();
}
catch (Exception e)
{

}
finally
{
context.ApplicationInstance.CompleteRequest();
}

I had the same issue, and was able to get it to work with FileDownload on the frontend:

const FileDownload = require('js-file-download');

postHtmlString(data).then(response => {
console.log(response)
commit('pdfDownloading', false)
FileDownload(response.data, 'example.pdf');
})...

You might also like