-1

Python code

from flask import Flask, app, render_template, request
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/after', methods=['GET', 'POST'])

def after():

    file = request.files['file1']
    file.save('static/file.jpg')
    return render_template('predict.html')

if __name__ == '__main__':
    app.run(debug = True)

following is html code from 'index.html':

<html>
    <form action="{{url_for('after')}}">
        <input type= "submit" enctype = 'multipart/form-data' method ="POST">
        <input type="file" name='file1'>
    </form>
</html>

And follwing is the code from 'predict.html'

<html>
    <body>
        <img src="{{url_for('static', filename = 'file.jpg')" alt="">
    </body>
</html>

I am trying to choose an image file by clicking on 'Choose file' button to submit it but while doing this I got the following error;

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'file1"

1 Answers1

2

Set the enctype and method attributes for your form tag, not for input:

<html>
    <form action="{{url_for('after')}}" enctype="multipart/form-data" method="POST">
        <input type="submit">
        <input type="file" name="file1">
    </form>
</html>
vremes
  • 654
  • 2
  • 7
  • 10