金字塔 - 上传图像并存储在磁盘上

Wiz*_*Wiz 2 python image uploading pyramid

我正在尝试实现一个将文件(图像)上传到运行金字塔的服务器的系统.现在,这段代码给了我一个AttributeError: 'unicode' object has no attribute 'file'例外:

服务器端:

session = Session()
username = authenticated_userid(request)
if username == None:
    return HTTPNotFound()
else:
    user = session.query(User).filter(User.username == username).first()
if 'photo.submitted' in request.params:
    input_file = request.POST['file_input'].file
    tmp = '../static/images/%s' % (session.query(ProfilePic).order_by(-ProfilePic.photo_id).first().photo_id + 1)
    open(tmp, 'w').write(input_file.read())
    tmp.close()
    return Response('OK')
return {}
Run Code Online (Sandbox Code Playgroud)

HTML:

<html>
<body>
    <form action="/settings" method="post">
        <input type="file" name="file_input" value="Choose image" />


    <p><input type="submit" name="photo.submitted" value="Save" /></p>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

看似简单但不起作用的东西.我试图按照教程,但它似乎只适用于视频/音频文件.我怎么能做这个工作?

Mar*_*ers 5

对于文件上载,您需要更改要使用的表单enctype multipart/form-data:

<html>
<body>
    <form action="/settings" method="post" enctype="multipart/form-data">
        <input type="file" name="file_input" value="Choose image" />


    <p><input type="submit" name="photo.submitted" value="Save" /></p>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)