Skip to content

Instantly share code, notes, and snippets.

@JanSuchanek
Last active August 29, 2015 13:55
Show Gist options
  • Save JanSuchanek/8710857 to your computer and use it in GitHub Desktop.
Save JanSuchanek/8710857 to your computer and use it in GitHub Desktop.
$.nette.ext('fileUpload', {
init: function(rh){
that = this;
$("form.fileUpload input[type=file]").on("change", this.onChangeFiles);
$("form.fileUpload").on("submit", this.onUploadSubmit);
},
before: function(jqXHR, settings){
var xhr = $.ajaxSettings.xhr();
xhr.upload.onprogress = function(e){
if(e.lengthComputable){
that.setPercent(Math.round(e.loaded/e.total*100));
}
};
xhr.upload.onload = function(){
that.setPercent(100);
};
return xhr;
},
success: function(payload){
setTimeout(function(){
that.getProgress().hide();
that.selector("input[type=file]").val('');
that.setPercent(0);
}, 2000);
}
}, {
onChangeFiles: function(event){
that.formFiles = this;
},
onUploadSubmit: function(event){
event.stopPropagation();
event.preventDefault();
that.uploadForm = this;
that.getProgress().show().addClass("progress-striped").addClass("active");
that.getBar().addClass("bar-success");
if(event.currentTarget.length > 0){
var formData = new FormData(this);
if(formData){
$.nette.ajax({
url: this.action,
type: 'POST',
data: formData,
dataType: 'json',
cache: false,
processData: false,
contentType: false,
});
}
}
},
selector: function(s){
return $(that.uploadForm).find(s)
},
getProgress: function(){
return that.selector('.progress');
},
getBar: function(){
return that.selector('.progress .progress-bar');
},
setPercent: function(p){
that.getBar().attr("aria-valuenow",p).width(p+'%').html(p+'% Complete');
},
formFiles: null,
uploadform: null
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment