Click here to Skip to main content
15,876,991 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have one url, for e.g., "https://www.[REDACTED].com/article/toDownload.pdf"

After pasting url in browser, PDF gets downloaded.

What I want is to download PDF and convert it into base64.


But after converting the code into base64, it does not show proper pdf format. It gets converted into base 64 but, after decoding it base64 to PDF online, it does not show valid format.
Can anyone help me with this??

string downloadPdf = "https://www.[REDACTED].com/article/toDownload.pdf";

 using (WebClient webClient = new WebClient())
	    webClient.Headers["Cache-Control"]= "public";
        webClient.Headers["Content-Description"] = "File Transfer";
        webClient.Headers["Content-Disposition"] = "attachment; 
        webClient.Headers["Content-Type"] = "application/pdf";
        webClient.Headers["Content-Transfer"] = "binary";
	    byte[] imageBytes = webClient.DownloadData(downloadPdf);
        var base64 = Convert.ToBase64String(imageBytes);

Online tool shows this error after decoding base64 to PDF:
"The MIME of file was detected as "application/octet-stream", but the decoder displays it as "application/pdf". To view it as is ,  Your browser cannot display the file as "application/pdf"."

What I have tried:

I tried different code that I got from Google, but am facing the same issue. I also used httpWebRequest in C#.
Updated 25-Oct-23 4:04am

1 solution

Start by checking what you actually download: what you will get from that likely to be an HTML page rather than a PDF file.
To check, the simplest way is to download the data and save it to a PDF file locally then open it in a PDF viewer and check it looks ok. If it does, then it's PDF data. If it doesn't ... use an editor to look at it, and you'll probably find out what it actually is.
Share this answer
Akshay malvankar 25-Oct-23 5:51am    
thanks issue resloved
OriginalGriff 25-Oct-23 6:35am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900