【发布时间】:2020-11-04 00:36:42
【问题描述】:
我正在尝试从服务器下载(即在服务器上创建文件的实例)一个 .pdf 文件,该文件以二进制格式返回给我,其中:
Content-Type = application / octet-stream.
经过一番网上研究,我来写:
http.get(url.parse(pdfURL), res => {
let data = [];
console.log(res.statusCode);
res.on('data', chunk => {
data.push(chunk);
}).on('end', () => {
let buffer = Buffer.concat(data);
console.log(buffer.toString('base64'));
fs.open(path, 'w', (e, fd) => {
if (e) throw e;
fs.write(fd, buffer, 0, buffer.length, null, e => {
if (e) throw e;
fs.close(fd, () => console.log('Wrote successfully'));
});
});
});
});
一切正常,但是当我尝试打开生成的 pdf 时,它告诉我文件已损坏且不可读。 知道可能出了什么问题吗?
谢谢
编辑: 我注意到邮递员一切正常,所以我认为我对待二进制文件的方式是错误的
【问题讨论】:
标签: node.js http pdf http-headers mime