简体   繁体   English

Flask Web应用程序无法上传文件

[英]Flask web application can't upload file

I am using the following code to upload a file to a directory on the server. 我正在使用以下代码将文件上传到服务器上的目录。 Each time I upload a file and press submit 'file' is not found in request.files. 每次我上载文件并按Submit时,request.files中都找不到。 Any ideas? 有任何想法吗?

views.py: 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: 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>

I believe your issue is with your value, I'd also recommend modifying your app so it prints request.files to clarify your response object. 我相信您的问题在于您的价值,我也建议您修改您的应用程序,以便它打印request.files来澄清您的响应对象。 In your jinja2 template, you should modify your code to specify: 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM