如何通过javascript下载pdf文件? [英] How to download a pdf file through javascript?

查看:2229
本文介绍了如何通过javascript下载pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript代码向我的node.js服务器发出以下AJAX请求:

My javascript code makes the following AJAX request to my node.js server:

var url = '/node/download';
var downloadRequest = new goog.net.XhrIo();
downloadRequest.headers.set('content-type', 'application/json');
downloadRequest.send(url);

我的node.js服务器在节点上创建一个pdf并通过以下方式将pdf流回客户端以下代码:

My node.js server creates a pdf at the node and streams the pdf back to the client via the following code:

    var filestream = fs.createReadStream(pdfpath);                  
    res.writeHead(200, {
        'Content-disposition': 'attachment; filename=' + filename,
        "Content-Type":"application/pdf","Content-Transfer-Encoding": "binary"});
    filestream.on('data', function(chunk) {                     
        res.write(chunk);
    });
    filestream.on('end', function() {
        res.end();
    });

但是现在我在如何在javascript客户端收到此回复以便下载时遇到问题提示将打开以允许用户下载并保存pdf文件。

But now I am having trouble at how to receive this response back at the javascript client so that a download prompt will open to allow the user to download and save the pdf file.

请帮忙!

Thanx in提前!

Thanx in advance!

PS Plz还建议任何更好的方法来实现我的节点代码(如果有的话)

P.S. Plz also suggest any better way to implement my node's code(if there is any)

编辑:一个可能的解决方案是发送我的请求:

One possible solution would be to send my request like this:

window.location.assign('/node/download');

这样我得到了下载提示,一切正常,只是牺牲了产品的异步性质。有没有解决这个问题,以便我也能保持异步性?

This way i get the download prompt and everything works fine except that the asynchronous nature of the product is sacrificed. Is there a work around for this so that I can also retain asynchronicity?

推荐答案

用于下载保存在的pdf文件服务器

从客户端javascript发出如下请求:

Make the request like this from the client javascript:

var reqObj = new XMLHttpRequest();
reqObj.open('GET','getpdf',true);     // 'getpdf' is the URI to recongize your request at the server
reqObj.send();

reqObj.onreadystatechange = function() {
    var resObj = this;
    if(resObj.readyState == resObj.DONE) {
        if (resObj.status != 200) {
            console.log("pdf can't be downloaded");
        } else if (resObj.status == 200){
            var resTxt = reqObj.responseText;
            window.location.assign(resTxt);    // Opens the pdf download prompt
        }
    }
}

在节点处理从上面收到的请求并回复:

At the node handle the request received from above and respond:

var http = require('http');

function getPDFUrl() {
    return "http://testing.com/pdf/test.pdf";
}

var handler = http.createServer(function(request, response) {
if (request.url == 'getpdf' && request.method.toLowerCase() == 'get') {
    var pdfUrl = getPDFUrl();       //get pdf url here

    if (pdfUrl != null && pdfUrl != undefined && pdfUrl != '') {
        response.writeHead(200, {"Content-Type":"text/html"});
        response.write(pdfUrl);
        response.end();
    } else {
        response.writeHead(404, {"Content-Type":"text/html"});
        response.write("Not Found");
        response.end();
    }

}
});

这篇关于如何通过javascript下载pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆