Skip to content

Commit 3f7a163

Browse files
author
Bartosz Kubicki
committed
Adding failure callback to ui file uploader
1 parent a3c2af0 commit 3f7a163

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

app/code/Magento/Ui/view/base/web/js/form/element/file-uploader.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ define([
5454
this.$fileInput = fileInput;
5555

5656
_.extend(this.uploaderConfig, {
57-
dropZone: $(fileInput).closest(this.dropZone),
58-
change: this.onFilesChoosed.bind(this),
59-
drop: this.onFilesChoosed.bind(this),
60-
add: this.onBeforeFileUpload.bind(this),
61-
done: this.onFileUploaded.bind(this),
62-
start: this.onLoadingStart.bind(this),
63-
stop: this.onLoadingStop.bind(this)
57+
dropZone: $(fileInput).closest(this.dropZone),
58+
change: this.onFilesChoosed.bind(this),
59+
drop: this.onFilesChoosed.bind(this),
60+
add: this.onBeforeFileUpload.bind(this),
61+
fail: this.onFail.bind(this),
62+
done: this.onFileUploaded.bind(this),
63+
start: this.onLoadingStart.bind(this),
64+
stop: this.onLoadingStop.bind(this)
6465
});
6566

6667
$(fileInput).fileupload(this.uploaderConfig);
@@ -328,11 +329,11 @@ define([
328329
* May be used for implementation of additional validation rules,
329330
* e.g. total files and a total size rules.
330331
*
331-
* @param {Event} e - Event object.
332+
* @param {Event} event - Event object.
332333
* @param {Object} data - File data that will be uploaded.
333334
*/
334-
onFilesChoosed: function (e, data) {
335-
// no option exists in fileuploader for restricting upload chains to single files; this enforces that policy
335+
onFilesChoosed: function (event, data) {
336+
// no option exists in file uploader for restricting upload chains to single files; this enforces that policy
336337
if (!this.isMultipleFiles) {
337338
data.files.splice(1);
338339
}
@@ -341,13 +342,13 @@ define([
341342
/**
342343
* Handler which is invoked prior to the start of a file upload.
343344
*
344-
* @param {Event} e - Event object.
345+
* @param {Event} event - Event object.
345346
* @param {Object} data - File data that will be uploaded.
346347
*/
347-
onBeforeFileUpload: function (e, data) {
348-
var file = data.files[0],
349-
allowed = this.isFileAllowed(file),
350-
target = $(e.target);
348+
onBeforeFileUpload: function (event, data) {
349+
var file = data.files[0],
350+
allowed = this.isFileAllowed(file),
351+
target = $(event.target);
351352

352353
if (this.disabled()) {
353354
this.notifyError($t('The file upload field is disabled.'));
@@ -356,7 +357,7 @@ define([
356357
}
357358

358359
if (allowed.passed) {
359-
target.on('fileuploadsend', function (event, postData) {
360+
target.on('fileuploadsend', function (eventBound, postData) {
360361
postData.data.append('param_name', this.paramName);
361362
}.bind(data));
362363

@@ -386,16 +387,25 @@ define([
386387
});
387388
},
388389

390+
/**
391+
* @param {Event} event
392+
* @param {Object} data
393+
*/
394+
onFail: function (event, data) {
395+
console.error(data.jqXHR.responseText);
396+
console.error(data.jqXHR.status);
397+
},
398+
389399
/**
390400
* Handler of the file upload complete event.
391401
*
392-
* @param {Event} e
402+
* @param {Event} event
393403
* @param {Object} data
394404
*/
395-
onFileUploaded: function (e, data) {
405+
onFileUploaded: function (event, data) {
396406
var uploadedFilename = data.files[0].name,
397-
file = data.result,
398-
error = file.error;
407+
file = data.result,
408+
error = file.error;
399409

400410
error ?
401411
this.aggregateError(uploadedFilename, error) :
@@ -469,10 +479,10 @@ define([
469479
* Handler of the preview image load event.
470480
*
471481
* @param {Object} file - File associated with an image.
472-
* @param {Event} e
482+
* @param {Event} event
473483
*/
474-
onPreviewLoad: function (file, e) {
475-
var img = e.currentTarget;
484+
onPreviewLoad: function (file, event) {
485+
var img = event.currentTarget;
476486

477487
file.previewWidth = img.naturalWidth;
478488
file.previewHeight = img.naturalHeight;

0 commit comments

Comments
 (0)