diff --git a/Resources/js/widget/file_upload.js b/Resources/js/widget/file_upload.js index 7f5fed04912b72dc015a700d7a999357c6a62410..a38120a8f8056cdb7cd86c29eb697444bcac82f2 100644 --- a/Resources/js/widget/file_upload.js +++ b/Resources/js/widget/file_upload.js @@ -47,17 +47,28 @@ options.acceptFileTypes = new RegExp('^' + options.acceptFileTypes + '$'); } - var updateDisplay = function() { - var hasEntry = false; - $entries.find('.fileinput-entry').each(function() { - var data = $(this).data('data'), - inError = !!(data && data.files.error); - $(this).toggleClass('alert alert-danger', inError); - $(this).find('.error').toggle(inError); - hasEntry = true; - }); - $button.toggle(options.multiple || !hasEntry); - }; + var showError = function(entry, message) { + var $this = $(entry); + if(message) { + $this.find('.error').text(message); + } + $this.addClass('alert alert-danger'); + $this.find('.error').show(); + $this.find('.icon') + .removeClass('fa-circle-o-notch fa-spin fa-file-o') + .addClass('fa-exclamation-triangle'); + }, + updateDisplay = function() { + var hasEntry = false; + $entries.find('.fileinput-entry').each(function() { + var data = $(this).data('data'); + if(data && data.files.error) { + showError(this, data.files[0].error); + } + hasEntry = true; + }); + $button.toggle(options.multiple || !hasEntry); + }; $this.find('.size').each(function() { $(this).text(formatFileSize($(this).text(), $(this).data('size-precision'))); @@ -83,7 +94,7 @@ row.find('.name').text(file.name); row.find('.size').text(formatFileSize(file.size)); if (file.error) { - row.find('.error').text(file.error); + showError(row, file.error); } rows = rows.add(row); }); @@ -96,7 +107,7 @@ row.find('.size').text(formatFileSize(file.size)); if (file.error) { row.find('.name').text(file.name); - row.find('.error').text(file.error); + showError(row, file.error); } else { row.find('.name a') .text(file.name) @@ -123,19 +134,19 @@ createUrl, { file: { name: file.name, size: file.size, type: file.type, lastModified: file.lastModified } }, function(response) { - if(response.status == 201) { - file.icon = response.icon; - data.url = response.put_url + csrfQuery; - data.delete_url = response.delete_url; - data.delete_type = response.delete_type; - data.jqXHR = $this.fileupload('send', data); - } else { - file.error = response.message || ('Error #' + response.status); - data.files.error = true; - data.context.find('.error').text(file.error); - } + file.icon = response.icon; + data.url = response.put_url + csrfQuery; + data.delete_url = response.delete_url; + data.delete_type = response.delete_type; + data.jqXHR = $this.fileupload('send', data); } - ); + ) + .fail(function(jqXHR, textStatus, errorThrown) { + file.error = textStatus === "error" ? errorThrown : ('Error #' + jqXHR.status); + data.files.error = true; + showError(data.context, file.error); + }) + .always(updateDisplay); return false; }, progress: function (e, data) {