After spending few hour on Jquery File Upload
I came to know that you can upload file in this way in order to have syn file upload
you can check the documentation here
Problem found
While doing data.submit() in add fileupload you are actually re-submiting
the form again n again therefore more than 1 ajax occurs.
Solution
HTML
Select video to upload:
<input id="fileupload" type="file" name="files[]" multiple>
<div id="files" class="files"></div>
JS
$(function () {
'use strict';
var url = 'server/php/',
uploadButton = $('<button/>')
.prop('disabled', true)
.on('click', function () {
var $this = $(this),
data = $this.data();
$this
.off('click')
.on('click', function () {
$this.remove();
data.abort();
});
data.submit().always(function () {
$this.remove();
});
});
$('#fileupload').fileupload({
url: url,
dataType: 'json',
autoUpload: false,
replaceFileInput:false,
acceptFileTypes: /(\.|\/)(mp4)$/i,
maxFileSize: 999000,
}).on('fileuploadadd', function (e, data) {
data.context = $('<div/>').appendTo('#files');
$.each(data.files, function (index, file) {
var node = $('<p/>')
if (!index) {
node.append(uploadButton.clone(true).data(data));
}
node.appendTo(data.context);
});
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index,
file = data.files[index],
node = $(data.context.children()[index]);
if (file.error) {
node
.append('<br>')
.append($('<span class="text-danger"/>').text(file.error));
}
if (index + 1 === data.files.length) {
data.context.find('button')
.text('Upload')
.prop('disabled', !!data.files.error);
}
})
});
Final Code will be :-
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/jquery.fileupload.css">
Select video to upload:
<input id="fileupload" type="file" name="files[]" multiple>
<div id="files" class="files"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="//blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script src="js/jquery.fileupload-process.js"></script>
<script src="js/jquery.fileupload-image.js"></script>
<script>
$(function () {
'use strict';
var url = 'server/php/',
uploadButton = $('<button/>')
.prop('disabled', true)
.on('click', function () {
var $this = $(this),
data = $this.data();
$this
.off('click')
.on('click', function () {
$this.remove();
data.abort();
});
data.submit().always(function () {
$this.remove();
});
});
$('#fileupload').fileupload({
url: url,
dataType: 'json',
autoUpload: false,
replaceFileInput:false,
acceptFileTypes: /(\.|\/)(mp4)$/i,
maxFileSize: 999000,
}).on('fileuploadadd', function (e, data) {
data.context = $('<div/>').appendTo('#files');
$.each(data.files, function (index, file) {
var node = $('<p/>')
if (!index) {
node.append(uploadButton.clone(true).data(data));
}
node.appendTo(data.context);
});
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index,
file = data.files[index],
node = $(data.context.children()[index]);
if (file.error) {
node
.append('<br>')
.append($('<span class="text-danger"/>').text(file.error));
}
if (index + 1 === data.files.length) {
data.context.find('button')
.text('Upload')
.prop('disabled', !!data.files.error);
}
})
});
</script>