python upload - tmp/FILES 在哪里?

发布于 2024-07-13 10:22:57 字数 343 浏览 6 评论 0原文

我正在从 cgi 运行 python 2.4,并尝试使用 python api 上传到云服务。 在 php 中,$_FILE 数组包含一个“tmp”元素,该元素是文件所在的位置,直到您将其放置在您想要的位置为止。 python 中的等价物是什么?

如果我这样做,

fileitem = form['file']

fileitem.filename 是文件的名称

(如果我打印 fileitem),则数组仅包含文件名和看起来是文件本身的内容。

我正在尝试传输内容,并且在使用 php api 时需要 tmp 位置。

I'm running python 2.4 from cgi and I'm trying to upload to a cloud service using a python api. In php, the $_FILE array contains a "tmp" element which is where the file lives until you place it where you want it. What's the equivalent in python?

if I do this

fileitem = form['file']

fileitem.filename is the name of the file

if i print fileitem, the array simply contains the file name and what looks to be the file itself.

I am trying to stream things and it requires the tmp location when using the php api.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-07-20 10:22:58

该文件是一个真实的文件,但 cgi.FieldStorage 在创建该文件后立即取消了它的链接,这样它仅在您保持打开状态时才存在,并且在该文件上不再具有真实路径文件系统。

但是,您可以更改此设置...

您可以扩展 cgi.FieldStorage 并替换 make_file 方法以将文件放置在您想要的任何位置:

import os
import cgi

class MyFieldStorage(cgi.FieldStorage):
    def make_file(self, binary=None):
        return open(os.path.join('/tmp', self.filename), 'wb')

您还必须记住FieldStorage 对象仅在收到超过 1000B 时才创建真实文件(否则它是 cStringIO.StringIO

编辑cgi 模块实际上使用 tempfile 模块生成文件,因此如果您需要大量详细信息,请检查一下。

The file is a real file, but the cgi.FieldStorage unlinked it as soon as it was created so that it would exist only as long as you keep it open, and no longer has a real path on the file system.

You can, however, change this...

You can extend the cgi.FieldStorage and replace the make_file method to place the file wherever you want:

import os
import cgi

class MyFieldStorage(cgi.FieldStorage):
    def make_file(self, binary=None):
        return open(os.path.join('/tmp', self.filename), 'wb')

You must also keep in mind that the FieldStorage object only creates a real file if it recieves more than 1000B (otherwise it is a cStringIO.StringIO)

EDIT: The cgi module actually makes the file with the tempfile module, so check that out if you want lots of gooey details.

寂寞陪衬 2024-07-20 10:22:58

这是从我的网站获取的代码片段:

h = open("user_uploaded_file", "wb")
while 1:
    data = form["file"].file.read(4096)
    if not data:
        break
    h.write(data)
h.close()

希望这有帮助。

Here's a code snippet taken from my site:

h = open("user_uploaded_file", "wb")
while 1:
    data = form["file"].file.read(4096)
    if not data:
        break
    h.write(data)
h.close()

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文