2012-07-31 66 views
2

我试图实现一个上传文件(图像)到运行金字塔的服务器的系统。眼下,这个代码给我一个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教程,但它似乎只适用于视频/音频文件。我怎么能做这个工作?

回答

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> 
+0

哇。我觉得很愚蠢。整个过程都在我面前。 – Wiz 2012-07-31 20:27:45