我有这个 PDF 文件,我需要将其转换为字节值数字数组。
我之前已将其转换为 Base64,但对于 2MB PDF,将 PDF 转换为 Base64 需要很长时间。 是否有另一种方法可以更快地将 PDF 转换为 Base64 或将该 PDF 文件转换为字节数组(看看它是否比转换为 Base64 更快)?
我之前使用 filereader 将文件读取到 dataURL 中。下面是我用来转换为 Base64 的代码。
getFileContentAsBase64(path, callback) {
window.resolveLocalFileSystemURL(path, gotFile, fail);
function fail(e) {
alert('Cannot found requested file');
}
function gotFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
var content = this.result;
callback(content);
}
reader.readAsDataURL(file);
});
}
}
编辑:
getFileContentAsBase64(path, callback) {
window.resolveLocalFileSystemURL(path, gotFile, fail);
function fail(e) {
alert('Cannot found requested file');
}
function gotFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
var content = this.result;
var array = new Uint8Array(content);
var binaryString = String.fromCharCode.apply(null, array);
callback(content);
}
reader.readAsArrayBuffer(file);
});
}
}
最佳答案
我通过将文件转换为二进制字符串来解决这个问题,然后我从服务器端调用云函数来进行文件的转换和保存。谢谢大家帮忙!~
关于javascript - 将文件转换为字节值数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43862211/