danielm / uploader

A lightweight and very configurable jQuery plugin for file uploading using ajax(a sync); includes support for queues, progress tracking and drag and drop.
https://danielmg.org/demo/java-script/uploader
MIT License
1.17k stars 384 forks source link

Add options for XHR errors #14

Closed itsthesteve closed 6 years ago

itsthesteve commented 9 years ago

By adding another hook object on the configuration of the widget, one get a better response from the server why something didn't work.

// ... defaults config
errorConfig : {
  source : 'xhr',
  param : 'responseJSON'
}

Later on (line ~255 of the src)

error: function (xhr, status, errMsg){
  var _msg;
  if(widget.settings.errorConfig.source == 'xhr'){
    _msg = xhr[widget.settings.errorConfig.param];
  }else{
    _msg = errMsg;
  }
  widget.settings.onUploadError.call(widget.element, widget.queuePos, _msg);
}...

Since there's no way to cancel the upload (so far I've seen...), i.e. returning false in any of the hook methods which would prevent further execution, one can set the HTTP error code from the server and whatever helpful message is needed to notify the admin or the user their path isn't going to work.

Now, this is a bit hacky, but for my use the response from the server, be it a success or a failure, will return an HTTP response code and a JSON message. A bit more massaging can allow the user to see exactly what happened instead of the generic HTTP header message, i.e. "Bad Request" or "Forbidden" based on the HTTP code supplied.