我使用以下代码将文件上传到服务器上的目录。每次我上传文件并按下提交'文件'都没有在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>
答案 0 :(得分:1)
我相信您的问题与您的价值有关,我还建议修改您的应用,以便打印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>