Browse Source

Add a multiUpload option to asset manager to allow forcing single file only uploads and configuring the exact filename in the form

pull/1430/head
Tom Robertson 7 years ago
parent
commit
c907ba746c
  1. 4
      docs/modules/Assets.md
  2. 4
      src/asset_manager/config/config.js
  3. 24
      src/asset_manager/view/FileUploader.js
  4. 12
      test/specs/asset_manager/view/FileUploader.js

4
docs/modules/Assets.md

@ -66,6 +66,10 @@ Below the list of currently available options
// 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:

4
src/asset_manager/config/config.js

@ -32,6 +32,10 @@ module.exports = {
// 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:

24
src/asset_manager/view/FileUploader.js

@ -7,7 +7,7 @@ module.exports = Backbone.View.extend(
template: _.template(`
<form>
<div id="<%= pfx %>title"><%= title %></div>
<input type="file" id="<%= uploadId %>" name="file" accept="*/*" <%= disabled ? 'disabled' : '' %> multiple/>
<input type="file" id="<%= uploadId %>" name="file" accept="*/*" <%= disabled ? 'disabled' : '' %> <%= multiUpload ? 'multiple' : '' %>/>
<div style="clear:both;"></div>
</form>
`),
@ -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;
@ -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
})
);

12
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: {

Loading…
Cancel
Save