Improve error reporting when file upload is not configured properly
This commit is contained in:
6
assets/js/app.min.js
vendored
6
assets/js/app.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -19,11 +19,17 @@ KB.component('file-upload', function (containerElement, options) {
|
||||
KB.find('#file-item-' + currentFileIndex).add(errorElement);
|
||||
}
|
||||
|
||||
function onServerError(response) {
|
||||
var errorElement = KB.dom('div').addClass('file-error').text(response.message).build();
|
||||
KB.find('#file-item-' + currentFileIndex).add(errorElement);
|
||||
KB.trigger('modal.stop');
|
||||
}
|
||||
|
||||
function onComplete() {
|
||||
currentFileIndex++;
|
||||
|
||||
if (currentFileIndex < files.length) {
|
||||
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError);
|
||||
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError, onServerError);
|
||||
} else {
|
||||
KB.trigger('modal.stop');
|
||||
KB.trigger('modal.hide');
|
||||
@@ -81,7 +87,7 @@ KB.component('file-upload', function (containerElement, options) {
|
||||
|
||||
function uploadFiles() {
|
||||
if (files.length > 0) {
|
||||
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError);
|
||||
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError, onServerError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,14 +83,25 @@ KB.http.postForm = function (url, formElement) {
|
||||
return (new KB.http.request('POST', url, {}, formData)).execute();
|
||||
};
|
||||
|
||||
KB.http.uploadFile = function (url, file, onProgress, onComplete, onError) {
|
||||
KB.http.uploadFile = function (url, file, onProgress, onComplete, onError, onServerError) {
|
||||
var fd = new FormData();
|
||||
fd.append('files[]', file);
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.upload.addEventListener('progress', onProgress);
|
||||
xhr.upload.addEventListener('load', onComplete);
|
||||
xhr.upload.addEventListener('error', onError);
|
||||
xhr.open('POST', url, true);
|
||||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
onComplete();
|
||||
} else if (typeof onServerError !== 'undefined') {
|
||||
onServerError(JSON.parse(xhr.responseText));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(fd);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user