简体   繁体   中英

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

I am working on a project where a pdf is dynamically created at the node server and needs to be sent to the javascript client where it should open a download prompt on the browser.

I have created the pdf but sending it and receiving it as downloadable is being troublesome.

Following is the code that I have used at the node to send the pdf but it doesn't seems to be right:

    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 at the javascript client to open the download prompt.

Currently all other responses from the node are being collected as:

var resp_json = printRequest.getResponseJson();

or

var resp_json = printRequest.getResponseText();

Can anyone help me?

PS I am using google closure library at the client (don't ask why, company MO).

Thanx in advance!!

尝试添加Content-Disposition:附件标头,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM