请求-承诺 下载 pdf 文件
request-promise download pdf file
我收到了多个 pdf 文件,必须从 REST-API 下载。
验证并连接后,我尝试使用请求承诺下载文件:
const optionsStart = {
uri: url,
method: 'GET',
headers: {
'X-TOKEN': authToken,
'Content-type': 'applcation/pdf'
}
}
request(optionsStart)
.then(function(body, data) {
let writeStream = fs.createWriteStream(uuid+'_obj.pdf');
console.log(body)
writeStream.write(body, 'binary');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
})
请求创建一个 pdf(大约 1-2MB),但我无法打开它。 (Mac 预览显示空白页和 adobe show = >
There was an error opening this document. There was a problem reading
this document (14).
我没有关于下载文件的 API 端点的信息。只要有这个卷曲:
curl -o doc.pdf --header "X-TOKEN: XXXXXX"
http://XXX.XXX/XXX/docs/doc1
我的错误在哪里?
更新:
我在编辑中打开文件,文件如下所示:
对此没有任何经验:-)
将编码:'binary' 添加到您的请求选项:
const optionsStart = {
uri: url,
method: "GET",
encoding: "binary", // it also works with encoding: null
headers: {
"Content-type": "application/pdf"
}
};
将 encoding: null
添加到您的请求选项中:
const optionsStart = {
uri: url,
method: "GET",
encoding: null,
headers: {
"Content-type": "application/pdf"
}
};
然后,将响应变成一个 Buffer(如果需要的话):
const buffer = Buffer.from(response);
试试这个
const optionsStart = {
uri: url,
method: 'GET',
headers: {
'X-TOKEN': authToken,
'Content-type': 'application/pdf'
},
encoding: null
}
request(optionsStart, (err, resp) => {
let writeStream = fs.createWriteStream(uuid + '_obj.pdf');
writeStream.write(resp.body, 'binary');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
})
我收到了多个 pdf 文件,必须从 REST-API 下载。
验证并连接后,我尝试使用请求承诺下载文件:
const optionsStart = {
uri: url,
method: 'GET',
headers: {
'X-TOKEN': authToken,
'Content-type': 'applcation/pdf'
}
}
request(optionsStart)
.then(function(body, data) {
let writeStream = fs.createWriteStream(uuid+'_obj.pdf');
console.log(body)
writeStream.write(body, 'binary');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
})
请求创建一个 pdf(大约 1-2MB),但我无法打开它。 (Mac 预览显示空白页和 adobe show = >
There was an error opening this document. There was a problem reading this document (14).
我没有关于下载文件的 API 端点的信息。只要有这个卷曲:
curl -o doc.pdf --header "X-TOKEN: XXXXXX"
http://XXX.XXX/XXX/docs/doc1
我的错误在哪里?
更新:
我在编辑中打开文件,文件如下所示:
对此没有任何经验:-)
将编码:'binary' 添加到您的请求选项:
const optionsStart = {
uri: url,
method: "GET",
encoding: "binary", // it also works with encoding: null
headers: {
"Content-type": "application/pdf"
}
};
将 encoding: null
添加到您的请求选项中:
const optionsStart = {
uri: url,
method: "GET",
encoding: null,
headers: {
"Content-type": "application/pdf"
}
};
然后,将响应变成一个 Buffer(如果需要的话):
const buffer = Buffer.from(response);
试试这个
const optionsStart = {
uri: url,
method: 'GET',
headers: {
'X-TOKEN': authToken,
'Content-type': 'application/pdf'
},
encoding: null
}
request(optionsStart, (err, resp) => {
let writeStream = fs.createWriteStream(uuid + '_obj.pdf');
writeStream.write(resp.body, 'binary');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
})