Search code examples
javascriptjquery-uiblueimp

BlueImp file upload plugin not working


I have a page that has the following input elemet:

<input class="uploadDocument" type="file" name="FileData" id="my-upl" />

And then the following javascript for starting the upload:

$("#my-upl").fileupload({
  url: uploadUrl,
  dataType: 'string',
  done: successFunction,
  uploadProgress: function(e, data) {
    var pct = parseInt(data.loaded / data.total * 100, 10);
    updateProgressBar(progressDiv, progressBar, progressLabel, Math.round(pct));
  },
  fail: function(e, data) {
    var errorHtml = uploadFailedHtml + "<p>" + data.textStatus + "</p>";
    showModal(uploadFailedTitle, errorHtml);
  }
});

It seems that the file upload does not trigger. I've even tried specifying a "start" and an "always" callback to see if anything gets triggered but to no avail.

EDIT: I've added an "add" callback and this does get triggered when the file is selected but the upload does not start.


Solution

  • The answer was found by looking at what code is run when using a button to start the upload

    In my add callback, I simply had to do data.submit():

    $("#my-upl").fileupload({
      url: uploadUrl,
      dataType: 'string',
      done: successFunction,
      uploadProgress: function(e, data) {
        var pct = parseInt(data.loaded / data.total * 100, 10);
        updateProgressBar(progressDiv, progressBar, progressLabel, Math.round(pct));
      },
      fail: function(e, data) {
        var errorHtml = uploadFailedHtml + "<p>" + data.textStatus + "</p>";
        showModal(uploadFailedTitle, errorHtml);
      },
      add: function(e, data) {
        data.submit();
        return true;
      }
    });