在python中本地下载文件 [英] Download file locally in python

查看:211
本文介绍了在python中本地下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OpenERP 中有一个生成 CSV 文件的python方法。我的问题是如何设置CSV的默认路径将是本地路径,而不是服务器上的路径。

I have a python method in OpenERP that generate a CSV file. My question is how can I set the default path of the CSV will be local path, not a path on the server.

我的一部分代码有这个 with open('export_data.csv','wb')as csvfile:。将文件保存在服务器上。我想要将该文件保存在客户端位置。

a part of my code have this with open('export_data.csv', 'wb') as csvfile:. which saves the file on the server. I want the file to be saved on the client side location.

您对如何实现这一点有任何想法吗?

Do you have any idea on how to achieve this?

openerp内置的导出数据功能不是一个选项。

the openerp built in export data function is not an option.

推荐答案

使用fields.binary



在OpenERP中客户端的二进制文件是使用 fields.binary 列。你可以把它放在一个 TransientModel 对象(以前 osv_memory ,用于创建交互式向导)的会话。二进制字段值可以被设置为先前操作的结果,或者可以通过 fields.function 列来模拟它来实时计算。

Using a fields.binary

One common way to return a binary file to the client side in OpenERP is to use a fields.binary column. You can put it in a TransientModel object (formerly osv_memory, used to create interactive wizards) that lives only for the duration of a session. The binary field value could be set as a result of a previous operation, or may be computed on-the-fly by having a fields.function column simulate it.

您可以在官方插件中找到几个例子,例如翻译导出向导,它生成CVS / PO / TGZ文件。一个缺陷是,二进制值必须以base64编码,因为历史RPC传输的原因。

You can find several examples of this in the official addons, such as the translation export wizard which produces CVS/PO/TGZ files. One pitfall is that the binary value must be base64 encoded, for historical RPC transfer reasons.

从OpenERP 6.1开始,还可以从服务器文件系统直接下载文件。这是简单的将文件的URL在 fields.char 值,作为一个相对的URL植根于您的模块,例如。 / module / path / to / file 。然后使用 widget =url在表单视图中将此字段添加为链接。使用这种方法的一种情况是配置向导< a>邮件客户端插件。

As of OpenERP 6.1 it is also possible to offer direct downloads of files from the server filesystem. It is as simple as putting the URL of the file in a fields.char value, as a relative URL rooted in your module, e.g. /module/path/to/file. Then add this field in a form view as a link, using widget="url". One case where this is used is the configuration wizard of the mail client plugins.

这适用于作为模块一部分的静态文件,但不适合由系统作为用户操作结果生成的文件,因为它们应该可能不会存储在模块的文件系统上,甚至暂时。在正确安全的安装中,OpenERP服务器进程甚至可能没有权限写入模块源所在的文件系统。

This works well for static files that are part of a module, but is not suited for files that are produced by the system as a result of user operations, because they should presumably not be stored on the module's filesystem, even temporarily. On a properly secured installation the OpenERP server process might not even have the right to write to the filesystem on which the module sources are located.

还有其他选择,例如编写自己的网络控制器,但上述解决方案在大多数情况下应该能够正常工作。

There are other options, such as writing your own web controller, but the above solutions should work well enough for most cases.

这篇关于在python中本地下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆