Split a big file to be upload using angular

岁酱吖の 提交于 2019-12-23 20:49:31

问题


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


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/27947752/split-a-big-file-to-be-upload-using-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!