Skip to content

Instantly share code, notes, and snippets.

@AndrewBuntsev
Created June 14, 2020 06:48
Show Gist options
  • Save AndrewBuntsev/a82462266432390d80d6e369292e4e21 to your computer and use it in GitHub Desktop.
Save AndrewBuntsev/a82462266432390d80d6e369292e4e21 to your computer and use it in GitHub Desktop.
function UploadVideo(file) {
var loaded = 0;
var chunkSize = 500000;
var total = file.size;
var reader = new FileReader();
var slice = file.slice(0, chunkSize);
// Reading a chunk to invoke the 'onload' event
reader.readAsBinaryString(slice);
console.log('Started uploading file "' + file.name + '"');
reader.onload = function (e) {
//Send the sliced chunk to the REST API
$.ajax({
url: "http://api/url/etc",
type: "POST",
data: slice,
processData: false,
contentType: false,
error: (function (errorData) {
console.log(errorData);
alert("Video Upload Failed");
})
}).done(function (e){
loaded += chunkSize;
var percentLoaded = Math.min((loaded / total) * 100, 100);
console.log('Uploaded ' + Math.floor(percentLoaded) + '% of file "' + file.name + '"');
//Read the next chunk and call 'onload' event again
if (loaded <= total) {
slice = file.slice(loaded, loaded + chunkSize);
reader.readAsBinaryString(slice);
} else {
loaded = total;
console.log('File "' + file.name + '" uploaded successfully!');
}
}
}
}
@AndrewBuntsev
Copy link
Author

AndrewBuntsev commented Apr 29, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment