3

I have to split a big file into 2mb parts to be sended into a server but i could not find any way. on angular or javascript. Right now i am using angularFileUpload to get it and send it as a big file. if anyone have a clue please let me know

  • Stream the file and read it in chunks maybe http://stackoverflow.com/questions/25810051/filereader-api-on-big-files – prawn Jan 14 '15 at 16:41

3 Answers3

4

You have to use the HTML5 file API. More info about it you can find here. I can't provide any code example, mainly because i don't know how your server looks. You have to give the user a transaction token, and he will have to send you the chunk number, chunk data and the token, so you'll be able to re-assemble it on the server.

Mihail Alexe
  • 340
  • 2
  • 9
0

You should be able to use FileAPI. I believe it provides a shim for older browsers that do not support the HTML5 File API.

Here's an example of it being used in the angular-file-upload repo.

Chance
  • 11,043
  • 8
  • 61
  • 84
0

You can try below code , This might help you to read file into chunks. in HTML file . Here Filereader is reading as text, but we can choose other way like reading as buffer etc.

and in ts file

uploadDoc(event) {
    let lastChunksize = 0;
    var file = event.target.files[0];

    this.readFile(file, lastChunksize, this.myCallback.bind(this));

   }

   myCallback(file, lastChunksize, result) {
    lastChunksize = lastChunksize + 20000;
    if(result) {
      //Add you logic what do you want after reading the file
      this.readFile(file, lastChunksize, this.myCallback.bind(this));
    } else {
      ///end recursion
    }
   }

   readFile(file,lastChunksize: number, callback) {

    var fileBlob = file.slice(lastChunksize,lastChunksize+20000);
    if(fileBlob.size !=0) {
      let fileReader = new FileReader();
      fileReader.onloadend= (result)=>{
      return callback(file,lastChunksize,fileReader.result)
      }
      fileReader.readAsText(fileBlob);
    }else {
     return callback(file,lastChunksize,false);
    }
   }