How to send downloadable pdf file to javascript client via node.js?

I am working on a project in which a pdf file is dynamically created on the node server and it must be sent to the javascript client, where it should open an invitation to download in the browser.

I created a pdf file, but send it and receive it, since the download becomes troublesome.

Below is the code that I used in node to send the PDF, but this does not seem to be correct:

    var pdf = fs.readFile("createdPdf/" + uname + "_44.pdf", function() {
        if (pdf == null) {
            res.writeHead(401, {"Content-Type": "text/html"});
            res.write('Failed');
            res.end();  
            return;
        } else {
            res.writeHead(200, {
                "Content-Type": "text/html"
            });
            res.write(pdf);
            res.end();
        }
    });

And I have no idea how to catch this pdf on the javascript client to open the download prompt.

Currently, all other answers from node are collected as:

var resp_json = printRequest.getResponseJson();

or

var resp_json = printRequest.getResponseText();

Can anybody help me?

PS I use the google closure library with the client (don't ask why, MO).

Thanx in advance!

+1
1

Content-Disposition: attachment, :

Content-Disposition: attachment; filename="thePdf.pdf"
+3

Source: https://habr.com/ru/post/1568242/


All Articles