diff --git a/addons/web/static/src/core/file_input/file_input.js b/addons/web/static/src/core/file_input/file_input.js index 2ebbb395222..80ddc25b6fd 100644 --- a/addons/web/static/src/core/file_input/file_input.js +++ b/addons/web/static/src/core/file_input/file_input.js @@ -34,6 +34,30 @@ export class FileInput extends Component { }); } + get httpParams() { + const { resId, resModel } = this.props; + const params = { + csrf_token: odoo.csrf_token, + ufile: [...this.fileInputRef.el.files], + }; + if (resModel) { + params.model = resModel; + } + if (resId !== undefined) { + params.id = resId; + } + return params; + } + + async uploadFiles(params) { + const fileData = await this.http.post(this.props.route, params, "text"); + const parsedFileData = JSON.parse(fileData); + if (parsedFileData.error) { + throw new Error(parsedFileData.error); + } + return parsedFileData; + } + //-------------------------------------------------------------------------- // Handlers //-------------------------------------------------------------------------- @@ -46,22 +70,7 @@ export class FileInput extends Component { * - resId: the id of the resModel target instance */ async onFileInputChange() { - const { resId, resModel, route } = this.props; - const params = { - csrf_token: odoo.csrf_token, - ufile: [...this.fileInputRef.el.files], - }; - if (resModel) { - params.model = resModel; - } - if (resId !== undefined) { - params.id = resId; - } - const fileData = await this.http.post(route, params, "text"); - const parsedFileData = JSON.parse(fileData); - if (parsedFileData.error) { - throw new Error(parsedFileData.error); - } + const parsedFileData = await this.uploadFiles(this.httpParams); this.props.onUpload(parsedFileData); }