6

odoo にカスタム Web フォームがあります。ファイルをアップロードする必要があります。私の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)

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

上記のコードは res_model などの名前の添付ファイルを作成しますが、添付ファイルは破損しており、開くことができません。

XML ファイル:

    ..........

<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>

コンソールでこれ:

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())

戻り値:

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')>
4

3 に答える 3

1

問題は base64.encode(file.read()) にあると思います

Python ドキュメントから
base64.encode(input, output)¶
入力ファイルの内容をエンコードし、base64 でエンコードされた結果のデータを出力ファイルに書き込みます。入力と出力は、ファイル オブジェクトまたはファイル オブジェクト インターフェイスを模倣するオブジェクトのいずれかでなければなりません。input は、input.read() が空の文字列を返すまで読み取られます。encode() は、エンコードされたデータと末尾の改行文字 ('\n') を返します。

したがって、この方法で使用して、
attachment = file.read()の
後に
'datas' を確認してください: attachment.encode('base64')

于 2015-08-10T09:57:38.493 に答える