Javascript 通过文件系统API导出文件时Chrome崩溃
我正在尝试在使用jQuery 1.10.2和CryptoJS 3.2.1的浏览器加密应用程序中运行 我面临的问题始于2mb左右的文件。文件可以很好地加密,但为文件创建数据URI时会使浏览器崩溃 我想有一种方法来解决这个问题,使它能够加密文件高达50mb的,而不会造成浏览器崩溃 下面是负责通过FileReader API保存文件的当前SNiPTJavascript 通过文件系统API导出文件时Chrome崩溃,javascript,google-chrome,encryption,html5-filesystem,Javascript,Google Chrome,Encryption,Html5 Filesystem,我正在尝试在使用jQuery 1.10.2和CryptoJS 3.2.1的浏览器加密应用程序中运行 我面临的问题始于2mb左右的文件。文件可以很好地加密,但为文件创建数据URI时会使浏览器崩溃 我想有一种方法来解决这个问题,使它能够加密文件高达50mb的,而不会造成浏览器崩溃 下面是负责通过FileReader API保存文件的当前SNiPT var reader = new FileReader(); if(body.hasClass('encrypt')){
var reader = new FileReader();
if(body.hasClass('encrypt')){
// Encrypt the file!
reader.onload = function(e){
// Use the CryptoJS library and the AES cypher to encrypt the
// contents of the file, held in e.target.result, with the password
var encrypted = CryptoJS.AES.encrypt(e.target.result, password);
// The download attribute will cause the contents of the href
// attribute to be downloaded when clicked. The download attribute
// also holds the name of the file that is offered for download.
a.attr('href', 'data:application/octet-stream,' + encrypted);
a.attr('download', file.name + '.encrypted');
step(4);
};
// This will encode the contents of the file into a data-uri.
// It will trigger the onload handler above, with the result
reader.readAsDataURL(file);
}
else {
// Decrypt it!
reader.onload = function(e){
var decrypted = CryptoJS.AES.decrypt(e.target.result, password)
.toString(CryptoJS.enc.Latin1);
if(!/^data:/.test(decrypted)){
alert("Invalid pass phrase or file! Please try again.");
return false;
}
a.attr('href', decrypted);
a.attr('download', file.name.replace('.encrypted',''));
step(4);
};
reader.readAsText(file);
}
我可以在上面的代码中更改什么以允许对较大的文件进行加密和解密
实时站点:(当前上限为1.5mb,否则保证浏览器崩溃)
请提前感谢。通过一点研究,我发现chrome中的数据url最多可以保存1.99MB 您的问题可以通过将数据url转换为blob来解决 您可以在此处找到更多信息: 这里是一个类似的帖子(见第二个答案) 编辑: 可能的解决办法
function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
var bb = new BlobBuilder();
bb.append(ab);
return bb.getBlob(mimeString);
}
function download(dataURI) {
var blob = dataURItoBlob(dataURI);
var url = window.URL.createObjectURL(blob);
window.location.assign(url);
}
函数dataURItoBlob(dataURI){
var byteString=atob(dataURI.split(',')[1]);
var mimeString=dataURI.split(',')[0]。split(':')[1]。split(';')[0]
var ab=新阵列缓冲区(byteString.length);
var ia=新的UINT8阵列(ab);
for(var i=0;i您可以通过调用
download(dataURI)
来使用此代码。我一直在研究BLOB,但我真的不知道如何实现BLOB。有点可怜,但我不是JS中最好的,也许是JS中最差的人。BlobBuilder
不受欢迎。您可以使用返回新Blob([ab],{type:mimeString})代码>取而代之。