var fileInput = document.getElementById("uploadVideoFile");
var fileInput = document.getElementById("uploadVideoFile");
var fileInput = document.getElementById("uploadVideoFile");
To display video we need a video element
div id="videoSourceWrapper">
<video style="width: 100%;" controls>
<source id="videoSource"/>
</video>
</div>
div id="videoSourceWrapper">
<video style="width: 100%;" controls>
<source id="videoSource"/>
</video>
</div>
div id="videoSourceWrapper">
<video style="width: 100%;" controls>
<source id="videoSource"/>
</video>
</div>
The following snippet displays the video after it is chosen in the video element for preview
$('#uploadVideoFile').on('change',
function() {
var fileInput = document.getElementById("uploadVideoFile");
console.log('Trying to upload the video file: %O', fileInput);
if ('files' in fileInput) {
if (fileInput.files.length === 0) {
alert("Select a file to upload");
} else {
var $source = $('#videoSource');
$source[0].src = URL.createObjectURL(this.files[0]);
$source.parent()[0].load();
$("#videoSourceWrapper").show();
}
} else {
console.log('No found "files" property');
}
}
);
$('#uploadVideoFile').on('change',
function(){
var fileInput = document.getElementById("uploadVideoFile");
console.log('Trying to upload the video file: %O', fileInput);
$('#uploadVideoFile').on('change',
function() {
var fileInput = document.getElementById("uploadVideoFile");
console.log('Trying to upload the video file: %O', fileInput);
if ('files' in fileInput) {
if (fileInput.files.length === 0) {
alert("Select a file to upload");
} else {
var $source = $('#videoSource');
$source[0].src = URL.createObjectURL(this.files[0]);
$source.parent()[0].load();
$("#videoSourceWrapper").show();
}
} else {
console.log('No found "files" property');
}
}
);
As you can see, you can watch any video file having all necessary controls in the video player. Now let's have a look how to upload the video on your server. For this purpose I have implemented the UploadVideo function
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!');
}
}
}
}
Hi,
ReplyDeleteWould you have an example of php code that could handle the backend of this?
Thanks!
Hi,
DeleteUnfortunately, I cannot help you with it, I implemented backend for the function with JavaScript.