Reputation: 187
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
Upvotes: 3
Views: 12613
Reputation: 1
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);
}
}
Upvotes: 0
Reputation: 11285
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.
Upvotes: 0
Reputation: 340
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.
Upvotes: 4