0

I have a nodeJS API configured which connects to another API and retrieves a data object, which is a Base64 string.

The client that calls the API endpoint needs to download a PDF file that is made from the base64 data.

How should the data be returned to the client so that it can be converted/prompt download from the browser? Should this be done client or server-side?

1
  • Does this answer your question? Commented Mar 21, 2020 at 21:58

1 Answer 1

1

Base64 is HTML safe and has utility when you want to create a data URL as a link. This would allow to preload the PDF so that the user instantly gets the file when clicking the link.

However, you just want to download it directly, so just convert it as binary on server side to reduce the network overhead related to base64, and keep the client as light as possible (load less data, use less resources).

Just set the proper content-type in the response (application/pdf if you want the PDF to be opened with the browser's PDF plugin, or application/octet-stream if you want to trigger a regular download) then just create a link to it with a classic <a href=.... You can also do one of the following:

Sign up to request clarification or add additional context in comments.

7 Comments

So the NodeJS connects to an old SOAP API. The soap API provides the base64 data object. Are you saying convert that to binary then send the binary as a response to the client? Or something else?
So how should the client handle the binary api response to trigger downloading a pdf file which is just data. That part I am unsure of. Thanks
Thanks, how do I set the content-type on the client side. At the moment the API is returning an object that includes FileName and the Data (binary or base64).
@StuartM you should not return an object, if you want to specify a filename do it in the Content-Disposition: attachment; filename="filename.pdf" then put the file as binary directly in the body
Is this all server side? Sorry I can’t find any info on doing this in JavaScript client side hence the original question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.