diff --git a/docs/modules/Assets.md b/docs/modules/Assets.md index 318b794e1..c1a390b24 100644 --- a/docs/modules/Assets.md +++ b/docs/modules/Assets.md @@ -63,6 +63,13 @@ Below the list of currently available options // Custom parameters to pass with the upload request, eg. csrf token params: {}, + // The credentials setting for the upload request, eg. 'include', 'omit' + credentials: 'include', + + // Allow uploading multiple files per request. + // If disabled filename will not have '[]' appended + multiUpload: true, + // If true, tries to add automatically uploaded assets. // To make it work the server should respond with a JSON containing assets // in a data key, eg: diff --git a/src/asset_manager/config/config.js b/src/asset_manager/config/config.js index 782122848..982967aaa 100644 --- a/src/asset_manager/config/config.js +++ b/src/asset_manager/config/config.js @@ -29,6 +29,13 @@ module.exports = { // Custom parameters to pass with the upload request, eg. csrf token params: {}, + // The credentials setting for the upload request, eg. 'include', 'omit' + credentials: 'include', + + // Allow uploading multiple files per request. + // If disabled filename will not have '[]' appended + multiUpload: true, + // If true, tries to add automatically uploaded assets. // To make it work the server should respond with a JSON containing assets // in a data key, eg: diff --git a/src/asset_manager/view/FileUploader.js b/src/asset_manager/view/FileUploader.js index d12584555..5130a7f26 100644 --- a/src/asset_manager/view/FileUploader.js +++ b/src/asset_manager/view/FileUploader.js @@ -7,7 +7,7 @@ module.exports = Backbone.View.extend( template: _.template(`
<%= title %>
- multiple/> + <%= multiUpload ? 'multiple' : '' %>/>
`), @@ -26,6 +26,7 @@ module.exports = Backbone.View.extend( c.disableUpload !== undefined ? c.disableUpload : !c.upload && !c.embedAsBase64; + this.multiUpload = c.multiUpload !== undefined ? c.multiUpload : true; this.events['change #' + this.uploadId] = 'uploadFile'; let uploadFile = c.uploadFile; @@ -78,7 +79,13 @@ module.exports = Backbone.View.extend( const em = this.config.em; const config = this.config; const target = this.target; - const json = typeof text === 'string' ? JSON.parse(text) : text; + let json; + try { + json = typeof text === 'string' ? JSON.parse(text) : text; + } catch (e) { + json = text; + } + em && em.trigger('asset:upload:response', json); if (config.autoAdd && target) { @@ -101,14 +108,18 @@ module.exports = Backbone.View.extend( const config = this.config; const params = config.params; - for (let i = 0; i < files.length; i++) { - body.append(`${config.uploadName}[]`, files[i]); - } - for (let param in params) { body.append(param, params[param]); } + if (this.multiUpload) { + for (let i = 0; i < files.length; i++) { + body.append(`${config.uploadName}[]`, files[i]); + } + } else if (files.length) { + body.append(config.uploadName, files[0]); + } + var target = this.target; const url = config.upload; const headers = config.headers; @@ -122,7 +133,7 @@ module.exports = Backbone.View.extend( this.onUploadStart(); return fetch(url, { method: 'post', - credentials: 'include', + credentials: config.credentials || 'include', headers, body }) @@ -229,6 +240,7 @@ module.exports = Backbone.View.extend( title: this.config.uploadText, uploadId: this.uploadId, disabled: this.disabled, + multiUpload: this.multiUpload, pfx: this.pfx }) ); diff --git a/test/specs/asset_manager/view/FileUploader.js b/test/specs/asset_manager/view/FileUploader.js index 20d72b368..8118286af 100644 --- a/test/specs/asset_manager/view/FileUploader.js +++ b/test/specs/asset_manager/view/FileUploader.js @@ -67,6 +67,18 @@ module.exports = { ); }); + test('Handles multiUpload false', () => { + var view = new FileUploader({ + config: { + multiUpload: false + } + }); + view.render(); + expect( + view.$el.find('input[type=file]').prop('multiple') + ).toBeFalsy(); + }); + test('Handles embedAsBase64 parameter', () => { var view = new FileUploader({ config: {