diff --git a/addons/web/static/src/views/widgets/attach_document/attach_document.js b/addons/web/static/src/views/widgets/attach_document/attach_document.js index e28cbf06ce2..3735e850aec 100644 --- a/addons/web/static/src/views/widgets/attach_document/attach_document.js +++ b/addons/web/static/src/views/widgets/attach_document/attach_document.js @@ -7,17 +7,47 @@ import { standardWidgetProps } from "@web/views/widgets/standard_widget_props"; import { Component } from "@odoo/owl"; -class AttachDocumentWidget extends Component { +export class AttachDocumentWidget extends Component { setup() { - this.orm = useService("orm"); + this.http = useService("http"); this.notification = useService("notification"); + + this.fileInput = document.createElement("input"); + this.fileInput.type = "file"; + this.fileInput.accept = "*"; + this.fileInput.multiple = true; + this.fileInput.onchange = this.onInputChange.bind(this); + } + + async onInputChange() { + const fileData = await this.http.post( + "/web/binary/upload_attachment", + { + csrf_token: odoo.csrf_token, + ufile: [...this.fileInput.files], + model: this.props.record.resModel, + id: this.props.record.resId, + }, + "text" + ); + const parsedFileData = JSON.parse(fileData); + if (parsedFileData.error) { + throw new Error(parsedFileData.error); + } + await this.onFileUploaded(parsedFileData); + } + + async triggerUpload() { + if (await this.beforeOpen()) { + this.fileInput.click(); + } } async onFileUploaded(files) { const { action, record } = this.props; if (action) { const { model, resId, resModel } = record; - await this.orm.call(resModel, action, [resId], { + await this.env.services.orm.call(resModel, action, [resId], { attachment_ids: files.map((file) => file.id), }); await record.load(); diff --git a/addons/web/static/src/views/widgets/attach_document/attach_document.xml b/addons/web/static/src/views/widgets/attach_document/attach_document.xml index c8528900425..d0fc0dac767 100644 --- a/addons/web/static/src/views/widgets/attach_document/attach_document.xml +++ b/addons/web/static/src/views/widgets/attach_document/attach_document.xml @@ -2,18 +2,8 @@