簡體   English   中英

金字塔-上傳圖像並存儲在磁盤上

[英]Pyramid - uploading images and storing on disk

我正在嘗試實現一個將文件(圖像)上傳到運行金字塔的服務器的系統。 現在,這段代碼給了我一個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>

看起來很簡單,但無法正常工作。 我試圖遵循教程,但似乎僅適用於視頻/音頻文件。 我該如何進行這項工作?

對於文件上傳,您需要更改表單編碼,以使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM