我发现问题是pdf文件的编码,所以我在后端使用'base64-stream'将文件编码为base64:
var readStream = fs.createReadStream(uploadFileFd);
// This will wait until we know the readable stream is actually valid before piping
readStream.on('open', function () {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(new Base64Encode()).pipe(res);
});
// This catches any errors that happen while creating the readable stream (usually invalid names)
readStream.on('error', function(err) {
res.end(err);
});
之后,我使用嵌入标签来显示 pdf:
var newElement = "<embed src=data:application/pdf;base64,"+data+" id='pdf' width='100%' height='1200'>";