[英]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.