我从node.js express服务器中的aws s3存储桶下载pdf,然后将该pdf写入uploads文件夹中的文件:
var s3 = new aws.S3();
var params = {
Bucket: 'aliro-pdf-assets',
Key: req.body.query
};
var out = fs.createWriteStream(__dirname + '/public/uploads/' + req.body.query);
s3.getObject(params).
on('httpData', function(chunk) { out.write(chunk); }).
on('httpDone', function() {
out.end();
res.send('http done');
}).send();
之后(在前端ajax调用下载此pdf的回调中),我尝试在前端渲染它,如下所示:
<object data="/uploads/' + pdata.pdf + '" width="98%" height="1300" type="application/pdf">
<p>You don\'nt have a PDF plugin for this browser. Click Download PDF up in the top left to view the PDF anyway!</p>\
</object>
也是这样的:
<a id="pdfDown" href="/uploads/' + pdata.pdf + '" class= "btn btn-danger btn-sm downloadPDF"><img src="images/pdfIcon.png">Download PDF</a>\
(pdata.pdf
是pdf的名称,与服务器中的req.body.query
相同)
问题是,这两个渲染都不起作用。 PDF.JS给了我这个错误:
Error: stream must have data pdf.worker.js:237:5
assert@resource://pdf.js/build/pdf.worker.js:254:5
init@resource://pdf.js/build/pdf.worker.js:2343:5
PDFDocument@resource://pdf.js/build/pdf.worker.js:2334:7
NetworkPdfManager@resource://pdf.js/build/pdf.worker.js:1996:24
getPdfManager@resource://pdf.js/build/pdf.worker.js:33775:24
wphSetupDoc@resource://pdf.js/build/pdf.worker.js:33979:7
messageHandlerComObjOnMessage@resource://pdf.js/build/pdf.worker.js:1247:9
pdf.worker.js:238:1
Warning: Unsupported feature "unknown" pdf.worker.js:224:5
Warning: Unsupported feature "unknown" pdf.js:224:5
An error occurred while loading the PDF.
PDF.js v1.1.551 (build: 2a5616c)
Message: stream must have data
怎么了?我知道我正在访问s3中的正确文件,但我在前端实际收到的pdf是零字节!