What is the best way for upload any type of Gigabyte File (such as: video,audio,image,document) to php server when using javascript and ajax?
send file as chunked blob?
Or first convert blob to ArrayBuffer then send to php?
how to merg file in Server Side when send file as ArrayBuffer ?
file to blob function :
function sliceFile(file, chunksAmount) {
var byteIndex = 0;
var chunks = [];
for (var i = 0; i < chunksAmount; i += 1) {
var byteEnd = Math.ceil((file.size / chunksAmount) * (i + 1));
chunks.push(file.slice(byteIndex, byteEnd));
byteIndex += (byteEnd - byteIndex);
}
return chunks;
}
send as ArrayBuffer function :
function sendChunkFile(chunksArray,filename){
var slice_chunk = chunksArray.shift();
var reader = new FileReader();
reader.onload = function(event){
var fd = new FormData();
fd.append('filename', filename);
fd.append('data', event.target.result);
$.ajax({
type: 'POST',
url: 'upload.php',
data: fd,
processData: false,
contentType: false
}).done(function(data) {
if(chunksArray.length){
sendChunkFile(chunksArray,filename)
}
});
};
reader.readAsArrayBuffer(slice_chunk);
}