如何保存此文件?

2024-03-28 20:15:18 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我提供了允许用户输入图像的HTML代码:

<label class="tab-item"> Browse
    <span class="icon icon-more"></span>
    <input type="file" accept="image/*"  name="image_save" value="image_save" onchange="updatePhoto(event);"></input>
</label>

#some more code

<form action="../api/save" method="post">
    <button type="submit" name="image_save" class="btn btn-positive btn-block" href="../api.put" value="image_save">
        Test Save
    </button>
</form>

在一个Python文件中:

@cherrypy.expose
def save(self, image_save=None):
    f = open(image_save)
    f.save("../photos/" + "test" + ".png", "PNG")
    return "Image saved"

但是我得到了错误

IOError: [Errno 2] No such file or directory: u'image_save'"

你们将如何保存通过HTML代码给出的图像?作为方法参数给出的是什么类型的数据?你知道吗


Tags: 代码name图像imageinputsavehtmlmore
1条回答
网友
1楼 · 发布于 2024-03-28 20:15:18

this site有一个类似的例子,它给出了一个python文件上传的例子:

#!/usr/local/bin/python
"""This demonstrates a minimal http upload cgi.
This allows a user to upload up to three files at once.
It is trivial to change the number of files uploaded.

This script has security risks. A user could attempt to fill
a disk partition with endless uploads. 
If you have a system open to the public you would obviously want
to limit the size and number of files written to the disk.
"""
import cgi
import cgitb; cgitb.enable()
import os, sys
try: # Windows needs stdio set for binary mode.
    import msvcrt
    msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
    msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
    pass

UPLOAD_DIR = "/tmp"

HTML_TEMPLATE = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body><h1>File Upload</h1>
<form action="%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
File name: <input name="file_1" type="file"><br>
File name: <input name="file_2" type="file"><br>
File name: <input name="file_3" type="file"><br>
<input name="submit" type="submit">
</form>
</body>
</html>"""

def print_html_form ():
    """This prints out the html form. Note that the action is set to
      the name of the script which makes this is a self-posting form.
      In other words, this cgi both displays a form and processes it.
    """
    print "content-type: text/html\n"
    print HTML_TEMPLATE % {'SCRIPT_NAME':os.environ['SCRIPT_NAME']}

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()

save_uploaded_file ("file_1", UPLOAD_DIR)
save_uploaded_file ("file_2", UPLOAD_DIR)
save_uploaded_file ("file_3", UPLOAD_DIR)

print_html_form ()

其他建议

我建议您将输入类型改为

 <input type="file" name="image_save" accept="image/x-png,image/gif,image/jpeg" />

而不是image/*以限制为png/jpg/gif。可以使用php检查MIME类型,我还建议您添加upload_max_filesize(在php中也是)

最后将fout行更改为

 fout = open(pathname, 'wb')

虽然file()构造函数是open()的别名,但为了将来和向后兼容,open()仍然是首选。你知道吗

相关问题 更多 >