在URL中打开二进制文件

时间:2014-09-25 01:03:36

标签: openerp-8

在文档模块中,当我单击二进制文件的链接时,它将作为JSON对象下载。

如何在Web浏览器中直接打开它。

我发现代码实现了文档模块的下载附件文件:

enter co@http.route('/web/binary/saveas_ajax', type='http', auth="user")
@serialize_exception
def saveas_ajax(self, data, token):
    jdata = simplejson.loads(data)
    model = jdata['model']
    field = jdata['field']
    data = jdata['data']
    id = jdata.get('id', None)
    filename_field = jdata.get('filename_field', None)
    context = jdata.get('context', {})

    Model = request.session.model(model)
    fields = [field]
    if filename_field:
        fields.append(filename_field)
    if data:
        res = { field: data }
    elif id:
        res = Model.read([int(id)], fields, context)[0]
    else:
        res = Model.default_get(fields, context)
    filecontent = base64.b64decode(res.get(field, ''))
    if not filecontent:
        raise ValueError(_("No content found for field '%s' on '%s:%s'") %
            (field, model, id))
    else:
        filename = '%s_%s' % (model.replace('.', '_'), id)
        if filename_field:
            filename = res.get(filename_field, '') or filename
        return request.make_response(filecontent,
            headers=[('Content-Type', 'application/octet-stream'),
                    ('Content-Disposition', content_disposition(filename))],
            cookies={'fileToken': token})de here

0 个答案:

没有答案