Answer the question
In order to leave comments, you need to log in
How to make verification of files uploaded to the server on flask?
Hello! Since 2018 everyone!) There was a question, how to check for a flask file? I need to check that the file is png and if it is not, fulfill one condition and stop the program, and also make a check that the image size should be 240 by 250 pixels and again, if not, return an error.
Now I have the following code:
import os
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/var/www/html/sample0/files'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = file.filename#secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return '''
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script>
$.ajax({
url : 'http://mysait.com/sample0/upload.php',
success : function(data) {
document.body.innerHTML = data;
}
});
</script>
'''
return '''
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script>
$.ajax({
url : 'http://mysait.com/sample0/upload.php',
success : function(data) {
document.body.innerHTML = data;
}
});
</script>
'''
if __name__ == "__main__":
app.run(debug=True)
Answer the question
In order to leave comments, you need to log in
Here you are right, you need to leave only png in AllowedExtensions
, if you only need PNG images, it's logical .
When uploading via HTTP POST, the content_type (Mime type) will be received. Need to check if "image/png" is.
As for the rest: as Mikhail Oleinikov answered .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question