[英]Flask web application can't upload file
我正在使用以下代碼將文件上傳到服務器上的目錄。 每次我上載文件並按Submit時,request.files中都找不到。 有任何想法嗎?
views.py:
@app.route("/upload_file")
def upload_file(self):
return self.render_template('upload_file.html')
@app.route("/file_uploader", methods=['GET', 'POST'])
def file_uploader(self):
if request.method == 'POST':
#application gets to this if block and returns 'No file part'
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
upload_file.html:
<h2>Please select a dataset to upload.</h2>
<form method=post enctype=multipart/form-data action={{ url_for('file_uploader')}}>
<span class="btn btn-primary btn-file"> Browse <input type="file"></span>
<input type=submit class="btn btn-success" value=Upload>
</form>
我相信您的問題在於您的價值,我也建議您修改您的應用程序,以便它打印request.files
來澄清您的響應對象。 在jinja2模板中,您應該修改代碼以指定:
<form method=post enctype=multipart/form-data action={{ url_for('file_uploader')}}>
<span class="btn btn-primary btn-file"> Browse
<input type=file name=file>
</span>
<input type=submit class="btn btn-success" value=Upload>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.