Upload files in Odoo web form
Asked Answered
Q

3

6

I have a custom web form in odoo. I need to upload files. My controllers.py:

@http.route(['/test/'], type='http', auth="user", methods=['GET', 'POST'], website=True)
def upload_files(self, **post):
    values = {}
    form_vals = {}

              ...........

    if post.get('attachment',False):
        Attachments = request.registry['ir.attachment']
        name = post.get('attachment').filename      
        file = post.get('attachment')
        attachment_id = Attachments.create(request.cr, request.uid, {
            'name':name,
            'res_name': name,
            'type': 'binary',
            'res_model': 'project.issue',
            'res_id': form_id,
            'datas': base64.encode(file.read()),
        }, request.context)

            ............

Code above creates attachment, with name res_model etc., but attached file iss damaged and can't be opened.

XML file:

    ..........

<form t-attf-action="/test/done" method="post" enctype="multipart/form-data" class="form-horizontal mt32"><div t-attf-class="form-group">

    ..........

    <div t-attf-class="form-group">
        <label class="col-md-3 col-sm-4 control-label" for="attachment">Attachments</label>
        <div class="col-md-7 col-sm-8">
            <input name="attachment" type="file" class="file" multiple="true" data-show-upload="true" data-show-caption="true" lass="file" data-show-preview="true"/>
        </div>
    </div>>

    ..........
</form>

In console this:

name = post.get('attachments_for_issue').filename
_logger.error("name is: %r", name)
file = post.get('attachments_for_issue')
_logger.error("file is?: %r", file.read())

returns:

5092 ERROR HDHDHD openerp.addons.test.controllers.controllers: name is: u'test_image.jpg'
5092 ERROR HDHDHD openerp.addons.test.controllers.controllers: file is?: <FileStorage: u'test_image.jpg' ('image/jpeg')>
Quirinal answered 9/8, 2015 at 17:5 Comment(5)
Try : enctype="multipart/form-data" To: "enctype="application/octet-stream"Faceplate
This doesn't work. Windows doesn't show what kind of error in log file, but page keeps loading and that's all.Quirinal
when a file not opened on python. You got EOF or header error(s). upload require right content header . You got a base64 object but i haven't any idea about of your saving procedure.Faceplate
Can you show more to show the controller.py and the xml file for more people to reference?Rorrys
Did this ever work for you ? I am trying exactly the same method but I get this error AttributeError: 'unicode' object has no attribute 'filename'Sweetener
Q
0

This is how it works:

file = post.get('attachment')
attach = file.stream
f = attach.getvalue()

and then:

...
'datas': base64.encodestring(f),
...

This adds file in attachment

Quirinal answered 11/8, 2015 at 13:53 Comment(1)
This doesn't work. There's no object with a name "post".Abuse
L
1

i think problem is with base64.encode(file.read())

from python docs we have
base64.encode(input, output)¶
Encode the contents of the input file and write the resulting base64 encoded data to the output file. input and output must either be file objects or objects that mimic the file object interface. input will be read until input.read() returns an empty string. encode() returns the encoded data plus a trailing newline character ('\n').

so, try to use in this way and check
attachment = file.read()
then
'datas' : attachment.encode('base64')

Lucrece answered 10/8, 2015 at 9:57 Comment(1)
attachment = file.read() Returns nothing, so I guess problem is there.Quirinal
Q
0

This is how it works:

file = post.get('attachment')
attach = file.stream
f = attach.getvalue()

and then:

...
'datas': base64.encodestring(f),
...

This adds file in attachment

Quirinal answered 11/8, 2015 at 13:53 Comment(1)
This doesn't work. There's no object with a name "post".Abuse
S
0

If getvalue() doesn't work or gives issues like it gave me, you can also try:

file = post.get('attachment').stream.read()

then:

'datas': base64.encodestring(file)
Sanguinary answered 14/12, 2018 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.