Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Download PDF from URL and convert it into base64 without saving it on a server

So basically what I want to do is to download a PDF file inside of a Web API from a URL I get as a parameter from the frontend and directly convert said file into a base64 string without saving the file on a file system.

I have already found WebClient.Download(URL, File) but that means, that I have to save the file.

So does anyone know any other solution that could work for me?

like image 365
V.Diesel Avatar asked Oct 23 '25 19:10

V.Diesel


1 Answers

You can use below code to download PDF from url into base64 string format.

string pdfUrl = "URL_TO_PDF";
using(WebClient client = new WebClient())
{
     var bytes = client.DownloadData(pdfUrl);
     string base64String = Convert.ToBase64String(bytes);
}
like image 161
Vijay Raheja Avatar answered Oct 26 '25 09:10

Vijay Raheja