Skip to content

Instantly share code, notes, and snippets.

@JanSuchanek
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save JanSuchanek/8710857 to your computer and use it in GitHub Desktop.

Select an option

Save JanSuchanek/8710857 to your computer and use it in GitHub Desktop.

Revisions

  1. JanSuchanek revised this gist Jan 30, 2014. 1 changed file with 5 additions and 8 deletions.
    13 changes: 5 additions & 8 deletions ext.uploadForm
    Original file line number Diff line number Diff line change
    @@ -26,19 +26,16 @@ $.nette.ext('fileUpload', {
    }, {
    onChangeFiles: function(event){
    that.formFiles = this;
    console.info(this.files.length)
    },
    onUploadSubmit: function(event){
    event.stopPropagation();
    event.preventDefault();


    that.uploadForm = this;

    that.getProgress().show().addClass("progress-striped").addClass("active");
    that.getBar().addClass("bar-success");

    console.info(this.files);
    if(event.currentTarget.length > 0){

    var formData = new FormData(this);
    @@ -47,11 +44,11 @@ $.nette.ext('fileUpload', {
    $.nette.ajax({
    url: this.action,
    type: 'POST',
    data: formData,
    dataType: 'json',
    cache: false,
    processData: false, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    data: formData,
    dataType: 'json',
    cache: false,
    processData: false,
    contentType: false,
    });
    }
    }
  2. JanSuchanek revised this gist Jan 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ext.uploadForm
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ $.nette.ext('fileUpload', {
    xhr.upload.onload = function(){
    that.setPercent(100);
    };
    return xhr;
    return xhr;
    },
    success: function(payload){
    setTimeout(function(){
  3. JanSuchanek revised this gist Jan 30, 2014. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions ext.uploadForm
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,6 @@ $.nette.ext('fileUpload', {
    };
    return xhr;
    },
    start: function(jqXHR, settings){
    return false;
    },
    success: function(payload){
    setTimeout(function(){
    that.getProgress().hide();
  4. JanSuchanek created this gist Jan 30, 2014.
    76 changes: 76 additions & 0 deletions ext.uploadForm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    $.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;
    },
    start: function(jqXHR, settings){
    return false;
    },
    success: function(payload){
    setTimeout(function(){
    that.getProgress().hide();
    that.selector("input[type=file]").val('');
    that.setPercent(0);
    }, 2000);
    }
    }, {
    onChangeFiles: function(event){
    that.formFiles = this;
    console.info(this.files.length)
    },
    onUploadSubmit: function(event){
    event.stopPropagation();
    event.preventDefault();


    that.uploadForm = this;

    that.getProgress().show().addClass("progress-striped").addClass("active");
    that.getBar().addClass("bar-success");

    console.info(this.files);
    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, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    });
    }
    }
    },
    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
    });