2012-07-31 44 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