简体   繁体   中英

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. Any ideas?

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>

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. In your jinja2 template, you should modify your code to specify:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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