首页 > 其他分享 >odoo 上传的文件怎样保存

odoo 上传的文件怎样保存

时间:2022-10-02 14:33:14浏览次数:45  
标签:self 保存 value filename ufile fname odoo 上传 datas

  1. 路由
@http.route('/web/binary/upload_attachment', type='http', auth="user")
@serialize_exception
def upload_attachment(self, callback, model, id, ufile):
files = request.httprequest.files.getlist('ufile')
Model = request.env['ir.attachment']
out = """<script language="javascript" type="text/javascript">
var win = window.top.window;
win.jQuery(win).trigger(%s, %s);
</script>"""
args = []
for ufile in files:

filename = ufile.filename
if request.httprequest.user_agent.browser == 'safari':
# Safari sends NFD UTF-8 (where é is composed by 'e' and [accent])
# we need to send it the same stuff, otherwise it'll fail
filename = unicodedata.normalize('NFD', ufile.filename)

try:
attachment = Model.create({
'name': filename,
'datas': base64.encodestring(ufile.read()),
'datas_fname': filename,
'res_model': model,
'res_id': int(id)
})
attachment._post_add_create()
except Exception:
args.append({'error': _("Something horrible happened")})
_logger.exception("Fail to upload attachment %s" % ufile.filename)
else:
args.append({
'filename': filename,
'mimetype': ufile.content_type,
'id': attachment.id
})
return out % (json.dumps(callback), json.dumps(args))
  1. 当文件内容保存在​​datas​​​ 字段中时触发​​inverse​​ 方法:
datas = fields.Binary(string='File Content', compute='_compute_datas', inverse='_inverse_datas')

def _inverse_datas(self):
location = self._storage()
for attach in self:
# compute the fields that depend on datas
value = attach.datas
bin_data = base64.b64decode(value) if value else b''
vals = {
'file_size': len(bin_data),
'checksum': self._compute_checksum(bin_data),
'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
'store_fname': False,
'db_datas': value,
}
if value and location != 'db':
# save it to the filestore
vals['store_fname'] = self._file_write(value, vals['checksum'])
vals['db_datas'] = False

# take current location in filestore to possibly garbage-collect it
fname = attach.store_fname
# write as superuser, as user probably does not have write access
super(IrAttachment, attach.sudo()).write(vals)
if fname:
self._file_delete(fname)

@api.model
def _file_write(self, value, checksum):
bin_value = base64.b64decode(value)
fname, full_path = self._get_path(bin_value, checksum)
if not os.path.exists(full_path):
try:
with open(full_path, 'wb') as fp:
fp.write(bin_value)
# add fname to checklist, in case the transaction aborts
self._mark_for_gc(fname)
except IOError:
_logger.info("_file_write writing %s", full_path, exc_info=True)
return fname

懂得,原来世界如此简单!



标签:self,保存,value,filename,ufile,fname,odoo,上传,datas
From: https://blog.51cto.com/qianxunman/5729319

相关文章

  • 此图形中的一个或多个对象无法保存为指定格式。操作未完成 ,因此未创建任何文件。
    “此图形中的一个或多个对象无法保存为指定格式。操作未完成,因此未创建任何文件。”这个有可能是你在用程序绘图时,画了0长度的线,或者0直径的圆等类似的情况。所以在函数......
  • Spring MVC入门(八):文件上传下载
    文件上传下载导入文件上传所需的依赖<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</versio......
  • 文件上传下载
    案例一demo为​​chnx/springboot/file-demo​​​​项目地址​​​​参考​​启动项目访问文件上传下载页面选择文件并上传后台打印出文件上传后的目录复制目录并查看文件......
  • 断点jsp-断点续传-大文件断点上传
    ​核心原理: 该项目核心就是文件分块上传。前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题。* 如何分片;* 如何合成一......
  • python sklearn 模型的保存与读取
    下载保存模块pipinstalljoblib导入模块import joblib保存模型joblib.dump(model,"model.joblib",compress=1)读取模型#加载模型文件,生成模型对象new_model=......
  • 让你的Mac iTerm优雅的上传下载文件
    我理解优雅是指支持拖拽、支持进度条、支持穿透容器/多层主机。 而今天的主角是trzsz. trzsz (trz/tsz)是一个兼容tmux的文件传输工具,和lrzsz(rz/sz)......
  • 网页大文件(百M以上)的上传下载实现技术
    ​我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所......
  • 如何通过secrets保存敏感的数据?并且在service中使用?
    什么是secrets? 前面的部分,你已经看到了config的使用方式,config中,存放的是明文的内容,以文件的方式挂载到容器中,今天的说的secret存放的是敏感的数据,比如,证书,key等 主......
  • 直播平台源代码,uni-app上传图片方法封装
    直播平台源代码,uni-app上传图片方法封装 functionchooseImg(count,success){console.log(count)uni.chooseImage({count:count,success:res=>{//console.log(re......
  • 直播平台软件开发,Vim自动缩进或保存时自动缩进
    直播平台软件开发,Vim自动缩进或保存时自动缩进一、自动缩进这非常方便,特别是如果正在使用Vim进行快速代码编辑,甚至进行长时间的编码会话。强制执行特定的缩进样式。在~/......