我正在尝试实施一个将文件(图像)上传到运行金字塔的服务器的系统。现在,这段代码给了我一个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 {}
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>
看似简单但却无法奏效的东西。我试图关注this教程,但它似乎只适用于视频/音频文件。我怎么能做这个工作?
答案 0 :(得分: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>