2

I'm trying for the first time to upload file with python, I tried using flask and werkzeug libraries, here is my code:

Here I'm creating the function to upload files:

@app.route('/upload')
def upload_file():
    return render_template('load.html')

@app.route('/uploader', methods=['GET','POST'])
@login_required(must=[be_admin, have_approval])
def uploaderV():
    if request.method == 'POST':
       file = request.files['file']
       if file:
          filename = secure_filename(file.filename)
          file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
          return 'file uploaded'
    return render_template('load.html')

Than this is my load.html page:

{% extends 'base.html' %}
{% block title %}Secret{% endblock %}
{% block page_body %}
     <div class="row">
          <form action="{{ url_for('uploaderV') }}" method="POST" enctype="multipart/form-data">
               <p>
                   <input type='file' name='file[]' multiple=''>
                   <input type="submit" value="Upload">
               </p>
          </form>
     </div>
{% endblock %}

Every time I try to upload a file the server gives me
werkzeug.exceptions.BadRequestKeyError BadRequestKeyError: 400 Bad Request: KeyError: 'file'
I tried in different ways, and now I really don't know what to do.

Alvin Nguyen
  • 250
  • 4
  • 10

1 Answers1

2

Your input name is file[] , not file. Try something like:

   file = request.files['file[]']

Or just change your input name to file.

Matt Healy
  • 18,033
  • 4
  • 56
  • 56