Browser crashes when upload file with Angular

2.1k Views Asked by At

I am trying to upload files in JSON using angular and FileReader Api.

The problem is that for files larger than 600 - 700 KB my browser crashes.

As far as I can see the problem occurs when requesting against a resource and not when the file reader reads the file and encode that in base64

Any ideas?

Here is the code:

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);
}

And here is the Resource Code:

crmApp.lazy.factory('UploadFilesResource',
['CrmAppResource', 'CrmAppConfiguration',
    function ($resource, CrmAppConfiguration) {
        return $resource(
            CrmAppConfiguration.apiUrl + 'upload/files/:id',{id:'@id'}
        );
    }
]);
1

There are 1 best solutions below

1
ibekiaris On

Thank you @Jeremy and @Jonas

That worked! The problem was JSON.stringify

So I removed that and now everything is OK with the browser!