我需要将 Base64 字符串解码为 PDF 文件。我正在使用这个代码。但window.atob命令总是报告该错误:无法在“Window”上执行“atob”:要解码的字符串未正确编码。
我知道该文件是正确的,因为我已经使用将 base64 解码为 pdf 的网站对其进行了解码。我不知道这是否有帮助,但我们正在使用 Aurelia 框架。
转换的函数
function converBase64toBlob(content, contentType) {
contentType = contentType || '';
var sliceSize = 512;
var byteCharacters = window.atob(content); //method which converts base64 to binary
var byteArrays = [
];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {
type: contentType
}); //statement which creates the blob
return blob;
}
Run Code Online (Sandbox Code Playgroud)
函数的调用
self.blob = self.converBase64toBlob(result.contents[0].pdf.replace(/^[^,]+,/, ''), 'application/pdf');
self.blobURL = URL.createObjectURL(blob);
window.open(this.blobURL);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26800 次 |
| 最近记录: |