I am using Flask Python to integrate a website with a python script. In such a page user should enter an image the this image is passed to the python script to do some processing on it. The problem here is that whenever I enter an image it goes throw
'file' not in request.files
I think problem in POST method but I can't get an answer to solve it. Flask code :
UPLOAD_FOLDER = path
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif']
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/services')
def services():
return render_template('services.html')
@app.route('/services', methods=['POST', 'GET'])
def upload_image():
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No image selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
full_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
print('save')
file.save(full_path)
# print('upload_image filename: ' + filename)
flash('Image successfully uploaded and displayed')
return render_template('services.html', filename=filename)
else:
flash('Allowed image types are -> png, jpg, jpeg, gif')
return redirect(request.url)
@app.route('/services/display/<filename>')
def display_image(filename):
# print('display_image filename: ' + filename)
return redirect(url_for('static', filename='uploads/' + filename), code=301)
HTML
<form method="post" action="" enctype="multipart/form-data">
<label for="img" style="margin-bottom: 40px;font-size: 30px">choose image:</label>
<input type="file" class="form-control col-lg" id="img" autocomplete="off" required name="img">
<p>
<input type="submit" value="Submit" name="submit">
</p>
</form>
Aucun commentaire:
Enregistrer un commentaire