2013-04-21 14 views
0

我想從request.form [「file」]寫入文件,但我無法做到。如何通過燒瓶從客戶端寫入png文件到目標目錄

我的contact.html在這裏。

客戶端代碼是這個樣子......

<form action="contact" method="POST" enctype="multipart/form-data"> 
<input type="file" name="file"> 
<input type="submit" value="submit"> 
</form> 

服務器端是這樣

filestorage = request.files["file"] 

print type(_file) #-> <FileStorage: u"__proto.png" ("img/png")> 

# I tried below this , but it doesn't work. 

f = open("tmp.png","wb") 
f.write(filestorage) 

我想寫這裏面是什麼地方png文件到上傳的文件。你有什麼主意嗎?

在此先感謝。

回答

1

首先,你必須配置你的上傳文件夾

app.config['UPLOAD_FOLDER'] = PATH_TO_UPLOAD_FOLDER 

然後保存文件

f = request.files["file"] 
f.save(os.path.join(app.config['UPLOAD_FOLDER'], 'tmp.png'))