【问题标题】:Upload a File with Python使用 Python 上传文件
【发布时间】:2012-08-23 08:50:14
【问题描述】:

我有一个 HTML 表单,我正在使用 Python 根据输入生成一个日志文件。如果用户愿意,我还希望能够允许用户上传图片。一旦它在那里,我可以弄清楚如何用 Python 操作它,但我不确定如何上传图像。这肯定是以前做过的,但我很难找到任何例子。你们谁能指出我正确的方向吗?

基本上,我使用cgi.FieldStoragecsv.writer 来制作日志。我想从用户的计算机中获取图像,然后将其保存到我服务器上的目录中。然后我将重命名它并将标题附加到 CSV 文件中。

我知道这有很多选择。我只是不知道它们是什么。如果有人可以指导我获取一些资源,我将非常感激。

【问题讨论】:

  • 如果您发布一些不起作用的代码,您更有可能得到答案,而不是笼统地问“我该怎么做”
  • 有很多方法可以做到这一点,使用不同的库......我们需要确切地知道你在使用什么。
  • 我不是在问“我该怎么做”。我知道这是不受欢迎的。我正在寻求一些资源的方向,所以我不会浪费每个人的时间。我将进行编辑以使其更清楚。
  • 是的。那是缺少的部分:-)
  • 已编辑。对于最初的防御感到抱歉。无知。 :/ 再次感谢!

标签: python image upload cgi


【解决方案1】:

既然您说您的特定应用程序是与 python cgi 模块一起使用的,那么快速 google 就会找到大量示例。这是第一个:

Minimal http upload cgi (Python recipe) (剪辑)

def save_uploaded_file (form_field, upload_dir):
    """This saves a file uploaded by an HTML form.
       The form_field is the name of the file input field from the form.
       For example, the following form_field would be "file_1":
           <input name="file_1" type="file">
       The upload_dir is the directory where the file will be written.
       If no file was uploaded or if the field does not exist then
       this does nothing.
    """
    form = cgi.FieldStorage()
    if not form.has_key(form_field): return
    fileitem = form[form_field]
    if not fileitem.file: return
    fout = file (os.path.join(upload_dir, fileitem.filename), 'wb')
    while 1:
        chunk = fileitem.file.read(100000)
        if not chunk: break
        fout.write (chunk)
    fout.close()

此代码将获取文件输入字段,这将是一个类似文件的对象。然后它将逐块读取到输出文件中。

2015 年 4 月 12 日更新:每个 cmets,我已经在这个旧的 activestate sn-p 的更新中添加了:

import shutil

def save_uploaded_file (form_field, upload_dir):
    form = cgi.FieldStorage()
    if not form.has_key(form_field): return
    fileitem = form[form_field]
    if not fileitem.file: return

    outpath = os.path.join(upload_dir, fileitem.filename)

    with open(outpath, 'wb') as fout:
        shutil.copyfileobj(fileitem.file, fout, 100000)

【讨论】:

  • fout = open(pathname, 'wb') 优于fout = file(pathname, 'wb');见Why is open() preferable over file() in Python?。更好的是:with open(pathname, 'wb') as fout:,然后当您离开 with 块的上下文时,fout 将自动关闭。
  • 也可以使用shutil.copyfileobj复制文件内容
  • 感谢 cmets。这是一个旧的 activestate 配方,我从中复制了片段。我已经更新了。
  • if not form.has_key(form_field) 不适用于 Python 3.5。请改用if form_field not in form
  • @MCF 是的,我记得他们在 py3 中弃用了 has_key
【解决方案2】:

网络框架工作 Pyramid 就是一个很好的例子。 http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/forms/file_uploads.html

这是我在一个工作项目中使用的示例代码。

    extension = os.path.splitext(request.POST[form_id_name].filename)[1]
    short_id = str(random.randint(1, 999999999))
    new_file_name =  short_id + extension
    input_file = request.POST[form_id_name].file
    file_path = os.path.join(os.environ['PROJECT_PATH'] + '/static/memberphotos/', new_file_name)

    output_file = open(file_path, 'wb')
    input_file.seek(0)
    while 1:
        data = input_file.read(2<<16)
        if not data:
            break
        output_file.write(data)
    output_file.close()

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 2010-11-20
    • 2021-02-08
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多