我正在尝试使用 Angular 和 FileReader Api
上传 JSON
中的文件。
问题是对于大于 600 - 700 KB
的文件,我的浏览器会崩溃。
据我所知,问题发生在请求资源时,而不是文件读取器读取文件并将其编码为 base64
有什么想法吗?
代码如下:
function readFiles(files) {
var reader = new FileReader();
var data = [];
function readFile(index) {
if (index >= files.length) {
UploadFilesResource.create(JSON.stringify(data), function (successData) {
scope.attachments = successData.data;
scope.showUploadForm = false;
}, function (errorData) {
MessageSrv.setErrorMessage(errorData.error_message)
});
return;
}
var file = files[index];
reader.onload = function(e) {
data.push(prepareFile(file, e.target.result));
readFile(index + 1)
};
reader.readAsDataURL(file);
}
readFile(0);
}
这是资源代码:
crmApp.lazy.factory('UploadFilesResource',
['CrmAppResource', 'CrmAppConfiguration',
function ($resource, CrmAppConfiguration) {
return $resource(
CrmAppConfiguration.apiUrl + 'upload/files/:id',{id:'@id'}
);
}
]);
最佳答案
谢谢@Jeremy 和@Jonas
成功了!问题是 JSON.stringify
所以我删除了它,现在浏览器一切正常!
关于javascript - 使用 Angular 上传文件时浏览器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782969/