2017-05-09 42 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:這個序列在加載腳本的時候保留了'