Skip to Content
Menu
This question has been flagged

I would like to allow a user to drag and drop a file onto span defined in data/dragdrop.xml . I managed to catch drop event but cannot make file upload. What do I have to do?


    <?xml version="1.0" encoding="UTF-8"?>
    <templates>
       <t t-name="mail.chatter.ChatComposer" t-extend="mail.chatter.ChatComposer">
           <t t-jquery=".o_composer_attachments_list" t-operation="before">
                <span class="o_chatter_composer_info" id="chatter_drop_target">
                    ~~~drag here~~~
                </span>
            </t>
        </t>
    </templates>


This is the code for module but it doesn't work. Where is my mistake in code? After drag and drop a file icon appears also in console I can see that file upload request status is 200. But the file icon is still showing "uploading" like the file didn't finish upload.


    odoo.define('dragdrop1.dragdrop', function(require) {
        "use strict";
        
    
        var core = require('web.core');
    
        var Composer = require('mail.composer');
    
        var MailThread = core.form_widget_registry.get('mail_thread');
    
        var ExtendedBasicComposer = Composer.BasicComposer.include({
            ondrop: function(event) {
                var form_upload = document.querySelector('form.o_form_binary_form');
    
                if (null === form_upload) {
                    return;
                }
    
                var form_data = new FormData(form_upload);
                var e = event.originalEvent;
                for (var iterator = 0, file; file = e.dataTransfer.files[iterator]; iterator++) {
                    if (file.name !== '') {
                        var filename = file.name.replace(/.*[\\\/]/,'');
                        // if the files exits for this answer, delete the file before upload
                        var attachments = [];
                        for (var i in this.get('attachment_ids')) {
                            if ((this.get('attachment_ids')[i].filename || this.get('attachment_ids')[i].name) === filename) {
                                if (this.get('attachment_ids')[i].upload) {
                                    //return false;
                                }
                                this.AttachmentDataSet.unlink([this.get('attachment_ids')[i].id]);
                            } else {
                                attachments.push(this.get('attachment_ids')[i]);
                            }
                        }
                        var form_data = new FormData(this.$('form.o_form_binary_form'));
                        form_data.set('ufile', file);
                        // submit filename
                        this.$('form.o_form_binary_form').submit();
                        this.$attachment_button.prop('disabled', true);
            
                        attachments.push({
                            'id': 0,
                            'name': filename,
                            'filename': filename,
                            'url': '',
                            'upload': true,
                            'mimetype': '',
                        });
                        this.set('attachment_ids', attachments);
                    }
                }
            },    
        });
    
        var NewChatterX = MailThread.include({
            on_open_composer_log_note: function(){
                this._super.apply(this, arguments);
                var self = this;
                var c = self.composer;
                var main = document.querySelector('body'); 
                $(document).on('drop', '#chatter_drop_target', function(event){
                    event.stopPropagation();
                    event.preventDefault();
                    c.ondrop(event);
                });
                $(document).on('dragenter', '#chatter_drop_target', function(event){
                    event.preventDefault();
                });
                $(document).on('dragover', '#chatter_drop_target', function(event){
                    event.stopPropagation();
                    event.preventDefault();
                });
                $(document).on('dragleave', '#chatter_drop_target', function(event){
                    event.preventDefault();
                });
            },
        });
        
    });

Any help appreciated, Thank you.

Avatar
Discard
Best Answer

Hi,

Why are you writing it when we have the module available for this feature?

Followings are the links of the modules:

OCA

ERP Harbor

Sudhir Arya
ERP Harbor Consulting Services
Skype:sudhir@erpharbor.com
Website: http://www.erpharbor.com
Avatar
Discard
Author Best Answer

Thank you. I didn't know before about those modules. I am trying to attach files to "internal notes" not the record currently open neither work with internal notes attachments unfortunately :/.

Avatar
Discard

What do you mean by internal notes?

Author

In Odoo chatter there is a link (next to "New Message") "Log an internal note". When clicked on "Log an internal note" the text box appears where I can write note, and there is also attachment button. When clicked on the attachment button then appears file choose window. Once I choose file it is immediately uploaded to the server as an attachment to that note. I would like to allow a user to drag and drop a file on the "Log an internal note" :) and once the file is dropped it should upload immediately. This would save users time because they will not have to browse to the file they would just drag and drop needed files. But "Drag and drop" is not supported in "Log an internal note" (in chatter), how to enable it? I tried with the source code above but files didn't upload, it's stuck :/

Best Answer

Hi Willmore,
I am also stuck with the same problem.
Have you figured out the solution yet?
If yes, please help me out in this, I will be really grateful.

Avatar
Discard
Related Posts Replies Views Activity
1
Feb 21
5838
1
Sep 20
5942
0
Oct 17
2261
5
Sep 19
11043
1
Sep 18
238