0
使用Meteor HTTP我可以从docusign获得响应并将其转换为base64缓冲区。从Docusign信封下载Blob API
try {
const response = HTTP.get(`${baseUrl}/envelopes/${envelopeId}/documents/1`, {
headers: {
"Authorization": `bearer ${token}`,
"Content-Type": "application/json",
},
});
const buffer = new Buffer(response.content).toString('base64');
return buffer
} catch(e) {
console.log(e);
throw new Meteor.Error(e.reason);
}
然后,我通过这个功能
function _base64ToArrayBuffer(base64) {
const binary_string = window.atob(base64);
const len = binary_string.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
// template helper
'click [data-action="download"]'(e, tmpl){
const doc = this;
return Meteor.call('downloadPDF', doc, (err, pdf)=>{
if(err) {
return notify({
message: err,
timeout: 3000,
})
}
const pdfBuffer = pdf && _base64ToArrayBuffer(pdf);
console.log(pdfBuffer);
return saveAs(new Blob([pdfBuffer], {type: 'application/pdf'}), `docusign_pdf.pdf`);
});
},
的PDF与正确的尺寸和页面长度下载使用FileSaver.js到saveAs
从ArrayBuffer创建一个blob的客户端,但所有的网页上是空白的。我应该以不同的方式编码缓冲区吗?还有什么我失踪?
好的,有趣的,谢谢。但是,如果我只是从api返回'result.content'并直接创建一个blob,它仍会下载正确大小的PDF和正确的页数,但所有页面都是空白的。 编辑:为了什么是值得的,我可以用邮差来打API,它下载文件就好了。 – xhaque
@xhaque - 因为它与邮递员一起工作,听起来像是你的流星设置的问题...... –
@xhaque:当你编码到base64时,尝试在下一行解码相同的内容,看看你是否看到相同的内容与否。看来base64的编码/解码正在破坏pdf。在'const buffer = new Buffer(response.content).toString('base64');'之后解码 –