Add "onUpload" AJAX handler to FileUpload widget (#4311)

Run an AJAX handler on uploading and processing a file as opposed to using a post() data check in the widget initialization, as the widget may initialize several times in certain circumstances - eg. inside a relation widget.

Credit to @alxy. Refs: https://github.com/octobercms/october/issues/4300
This commit is contained in:
Alexander Guth 2019-05-07 05:44:25 +02:00 committed by Ben Thomson
parent 4e92686c1a
commit ee8287e598
6 changed files with 9 additions and 14 deletions

View File

@ -113,7 +113,6 @@ class FileUpload extends FormWidgetBase
} }
$this->getConfigFormWidget(); $this->getConfigFormWidget();
$this->checkUploadPostback();
} }
/** /**
@ -410,15 +409,10 @@ class FileUpload extends FormWidgetBase
} }
/** /**
* Checks the current request to see if it is a postback containing a file upload * Upload handler for the server-side processing of uploaded files
* for this particular widget.
*/ */
protected function checkUploadPostback() public function onUpload()
{ {
if (!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->getId()) {
return;
}
try { try {
if (!Input::hasFile('file_data')) { if (!Input::hasFile('file_data')) {
throw new ApplicationException('File missing from request'); throw new ApplicationException('File missing from request');
@ -482,8 +476,7 @@ class FileUpload extends FormWidgetBase
$response = Response::make($ex->getMessage(), 400); $response = Response::make($ex->getMessage(), 400);
} }
// Override the controller response return $response;
$this->controller->setResponse($response);
} }
/** /**

View File

@ -114,10 +114,6 @@
this.uploaderOptions.previewTemplate = $(this.options.template).html() this.uploaderOptions.previewTemplate = $(this.options.template).html()
} }
if (this.options.uniqueId) {
this.uploaderOptions.headers['X-OCTOBER-FILEUPLOAD'] = this.options.uniqueId
}
this.uploaderOptions.thumbnailWidth = this.options.thumbnailWidth this.uploaderOptions.thumbnailWidth = this.options.thumbnailWidth
? this.options.thumbnailWidth : null ? this.options.thumbnailWidth : null
@ -192,6 +188,7 @@
FileUpload.prototype.onUploadSending = function(file, xhr, formData) { FileUpload.prototype.onUploadSending = function(file, xhr, formData) {
this.addExtraFormData(formData) this.addExtraFormData(formData)
xhr.setRequestHeader('X-OCTOBER-REQUEST-HANDLER', this.options.uploadHandler)
} }
FileUpload.prototype.onUploadSuccess = function(file, response) { FileUpload.prototype.onUploadSuccess = function(file, response) {
@ -428,6 +425,7 @@
FileUpload.DEFAULTS = { FileUpload.DEFAULTS = {
url: window.location, url: window.location,
uploadHandler: null,
configHandler: null, configHandler: null,
sortHandler: null, sortHandler: null,
uniqueId: null, uniqueId: null,

View File

@ -2,6 +2,7 @@
id="<?= $this->getId() ?>" id="<?= $this->getId() ?>"
class="field-fileupload style-file-multi is-sortable is-multi <?= count($fileList) ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>" class="field-fileupload style-file-multi is-sortable is-multi <?= count($fileList) ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>"
data-control="fileupload" data-control="fileupload"
data-upload-handler="<?= $this->getEventHandler('onUpload') ?>"
data-template="#<?= $this->getId('template') ?>" data-template="#<?= $this->getId('template') ?>"
data-error-template="#<?= $this->getId('errorTemplate') ?>" data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-sort-handler="<?= $this->getEventHandler('onSortAttachments') ?>" data-sort-handler="<?= $this->getEventHandler('onSortAttachments') ?>"

View File

@ -2,6 +2,7 @@
id="<?= $this->getId() ?>" id="<?= $this->getId() ?>"
class="field-fileupload style-file-single <?= $singleFile ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>" class="field-fileupload style-file-single <?= $singleFile ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>"
data-control="fileupload" data-control="fileupload"
data-upload-handler="<?= $this->getEventHandler('onUpload') ?>"
data-template="#<?= $this->getId('template') ?>" data-template="#<?= $this->getId('template') ?>"
data-error-template="#<?= $this->getId('errorTemplate') ?>" data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-unique-id="<?= $this->getId() ?>" data-unique-id="<?= $this->getId() ?>"

View File

@ -2,6 +2,7 @@
id="<?= $this->getId() ?>" id="<?= $this->getId() ?>"
class="field-fileupload style-image-multi is-sortable is-multi <?= count($fileList) ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>" class="field-fileupload style-image-multi is-sortable is-multi <?= count($fileList) ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>"
data-control="fileupload" data-control="fileupload"
data-upload-handler="<?= $this->getEventHandler('onUpload') ?>"
data-template="#<?= $this->getId('template') ?>" data-template="#<?= $this->getId('template') ?>"
data-error-template="#<?= $this->getId('errorTemplate') ?>" data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-sort-handler="<?= $this->getEventHandler('onSortAttachments') ?>" data-sort-handler="<?= $this->getEventHandler('onSortAttachments') ?>"

View File

@ -2,6 +2,7 @@
id="<?= $this->getId() ?>" id="<?= $this->getId() ?>"
class="field-fileupload style-image-single <?= $singleFile ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>" class="field-fileupload style-image-single <?= $singleFile ? 'is-populated' : '' ?> <?= $this->previewMode ? 'is-preview' : '' ?>"
data-control="fileupload" data-control="fileupload"
data-upload-handler="<?= $this->getEventHandler('onUpload') ?>"
data-template="#<?= $this->getId('template') ?>" data-template="#<?= $this->getId('template') ?>"
data-error-template="#<?= $this->getId('errorTemplate') ?>" data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-unique-id="<?= $this->getId() ?>" data-unique-id="<?= $this->getId() ?>"