javascript - Split a big file to be upload using angular - Stack Overflow

I have to split a big file into 2mb parts to be sended into a server but i could not find any way. on a

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

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

Share Improve this question edited Jan 14, 2015 at 17:47 Miguel Garcia Boriani asked Jan 14, 2015 at 16:27 Miguel Garcia BorianiMiguel Garcia Boriani 1871 gold badge2 silver badges9 bronze badges 1
  • Stream the file and read it in chunks maybe stackoverflow./questions/25810051/… – prawn Commented Jan 14, 2015 at 16:41
Add a ment  | 

3 Answers 3

Reset to default 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.

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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742338674a4425174.html

相关推荐

  • javascript - Split a big file to be upload using angular - Stack Overflow

    I have to split a big file into 2mb parts to be sended into a server but i could not find any way. on a

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信