Hello Everyone,
I am getting output in base64 format, I want to convert and download it into pdf and save to my local pc downloads folder.
I have converted that pdf to Bytes and tried to download using CreateDirectory method, but it downloads pdf to server's folder.
I am looking sample code in .net
Thanks in advance !!

Jignesh KumarRank #91 · 22.9K reputationPosted Nov 30, 2024, 12:40 PM EST
Hello Shusant,
using the below code you can create a pdf from the base64String.
try { // Decode the base64 string to a byte array byte[] pdfBytes = Convert.FromBase64String(base64String); // Set file name and content type string fileName = "ConvertedFile.pdf"; string contentType = "application/pdf"; // Return the file to the client return File(pdfBytes, contentType, fileName); } catch (FormatException ex) { return BadRequest($"Invalid base64 string: {ex.Message}"); } catch (Exception ex) { return StatusCode(500, $"An error occurred: {ex.Message}"); }You can write client-side code to download,
function downloadBase64Pdf(base64String) { fetch('/File/DownloadPdf', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(base64String), }) .then((response) => { if (!response.ok) { throw new Error('Failed to download PDF.'); } return response.blob(); }) .then((blob) => { const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = 'ConvertedFile.pdf'; document.body.appendChild(link); link.click(); link.remove(); }) .catch((error) => { console.error('Error:', error); }); } // Example usage const base64String = "JVBERi0xLjQKJ...."; // Replace with your base64 string downloadBase64Pdf(base64String);Sangeetha SRank #212 · 11.4K reputationPosted Dec 2, 2024, 3:19 AM EST
Base64 String: Ensure that you have your PDF content as a Base64 string.
File Name: Specify the desired file name (e.g., "document.pdf").
Example
protected void Page_Load(object sender, EventArgs e) { string base64String = "YourBase64EncodedPDFHere"; string fileName = "downloadedDocument.pdf"; PdfDownloader downloader = new PdfDownloader(); downloader.DownloadPdf(base64String, fileName); }using System; using System.IO; using System.Web; public class PdfDownloader { public void DownloadPdf(string base64String, string fileName) { // Convert Base64 string to byte array byte[] pdfBytes = Convert.FromBase64String(base64String); //Set the response content type to PDF HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("Content-Disposition", $"attachment; filename={fileName}"); HttpContext.Current.Response.BinaryWrite(pdfBytes); HttpContext.Current.Response.End(); } }Sushant TorankarRank #3,026 · 476 reputationPosted Dec 2, 2024, 11:22 AM EST
Hello Jignesh,
Thanks for reply.
Your code looks like MVC based code, which does not work in c# asp.net . I am looking for a code in c# .net code. Which will convert base64 to pdf file and then download that pdf to DOWNLOADS folder using RESPONSE property.
Sushant TorankarRank #3,026 · 476 reputationPosted Dec 2, 2024, 11:23 AM EST
Hello Sangitha,
Thanks for reply.
I'll try your code and let you know
Naimish MakwanaRank #324 · 7.7K reputationPosted Dec 3, 2024, 12:24 AM EST
Here's a sample code snippet in C# that converts a Base64 string to a PDF file and then downloads it to the user's Downloads folder using the
Responseproperty in an ASP.NET application:using System; using System.IO; using System.Web; public class PdfHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { // Base64 string (replace with your actual Base64 string) string base64String = "your_base64_string_here"; // Convert Base64 string to byte array byte[] pdfBytes = Convert.FromBase64String(base64String); // Set the response content type to PDF context.Response.ContentType = "application/pdf"; // Set the content disposition to attachment to prompt download context.Response.AddHeader("Content-Disposition", "attachment; filename=downloadedFile.pdf"); // Write the byte array to the response context.Response.BinaryWrite(pdfBytes); // End the response context.Response.End(); } public bool IsReusable { get { return false; } } }Explanation:
Convert.FromBase64Stringmethod converts the Base64 string to a byte array.context.Response.ContentTypeis set to "application/pdf" to indicate the type of file being sent.context.Response.AddHeadermethod sets the "Content-Disposition" header to "attachment" with a filename, prompting the browser to download the file.context.Response.BinaryWritemethod writes the byte array to the response.context.Response.Endmethod ends the response, ensuring the file is sent to the client.Usage:
This approach ensures the PDF is downloaded directly to the client's machine using the
Responseproperty.Thanks