diff --git a/src/asset_manager/config/config.js b/src/asset_manager/config/config.js index 982967aaa..63afd77c7 100644 --- a/src/asset_manager/config/config.js +++ b/src/asset_manager/config/config.js @@ -55,7 +55,17 @@ module.exports = { // Label for the add button addBtnText: 'Add image', - // Custom uploadFile function + // To upload your assets, the module uses Fetch API, with this option you + // overwrite it with something else. + // It should return a Promise + // @example + // customFetch: (url, options) => axios(url, { data: options.body }), + customFetch: '', + + // Custom uploadFile function. + // Differently from the `customFetch` option, this gives a total control + // over the uploading process, but you also have to emit all `asset:upload:*` events + // by yourself (if you need to use them somewhere) // @example // uploadFile: (e) => { // var files = e.dataTransfer ? e.dataTransfer.files : e.target.files; diff --git a/src/asset_manager/view/FileUploader.js b/src/asset_manager/view/FileUploader.js index 5130a7f26..a4aac4d1d 100644 --- a/src/asset_manager/view/FileUploader.js +++ b/src/asset_manager/view/FileUploader.js @@ -104,9 +104,9 @@ module.exports = Backbone.View.extend( * */ uploadFile(e, clb) { const files = e.dataTransfer ? e.dataTransfer.files : e.target.files; + const { config } = this; const body = new FormData(); - const config = this.config; - const params = config.params; + const { params, customFetch } = config; for (let param in params) { body.append(param, params[param]); @@ -131,18 +131,20 @@ module.exports = Backbone.View.extend( if (url) { this.onUploadStart(); - return fetch(url, { + const fetchOpts = { method: 'post', credentials: config.credentials || 'include', headers, body - }) - .then( - res => + }; + const fetchResult = customFetch + ? customFetch(url, fetchOpts) + : fetch(url, fetchOpts).then(res => ((res.status / 200) | 0) == 1 ? res.text() : res.text().then(text => Promise.reject(text)) - ) + ); + return fetchResult .then(text => this.onUploadResponse(text, clb)) .catch(err => this.onUploadError(err)); }