2017-05-09 40 views
1

我有这个PDF文件,我需要将其转换为字节数值数组。将文件转换为字节数值数组

我以前已经将它转换为base64,但PDF转换为base64花费了2MB PDF。 是否有另一种方法可以更快地将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); 
    }); 
} 

} 
+0

http://stackoverflow.com/a/32556944/1848109我想soln已经回答了。 – Nirus

+0

@Nirus我已经看到了这个解决方案,但它似乎不适用于我的情况。我上面编辑了我的问题,向你展示我所尝试过的。也许我的逻辑背后的逻辑是错误的...... – Lyon

+0

@Lyon:这个序列在加载脚本的时候保留了'