4

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?

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
V.Diesel
  • 77
  • 1
  • 1
  • 7

2 Answers2

12

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);
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
Vijay Raheja
  • 290
  • 2
  • 10
0

as said in first answer of Downloading pdf file using WebRequests

var fileName = "output/" + date.ToString("yyyy-MM-dd") + ".pdf";
using (var stream = File.Create(fileName))
 resp.GetResponseStream().CopyTo(stream);

(resp is

 HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

)

then convert to base64

 String file = Convert.ToBase64String(stream);
Xavave
  • 645
  • 11
  • 15