Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from flask import Flask, jsonify, request, make_response
- from flask_cors import CORS
- from werkzeug.utils import secure_filename
- UPLOAD_FOLDER='/images'
- ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
- app = Flask(__name__)
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
- CORS(app)
- def allowed_file(filename):
- return '.' in filename and \
- filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
- @app.route('/', methods=['GET', 'POST'])
- def upload_file():
- print(request.files)
- if request.method == "POST":
- # check if the post request has the file part
- if 'file' not in request.files:
- print('No file part')
- return make_response("No File Part", 400)
- file = request.files["file"]
- # if user does not select file, browser also submit an empty part
- # without filename
- if file.filename == '':
- print('No selected file')
- return make_response("No Selected File", 400)
- if file and allowed_file(file.filename):
- filename = secure_filename(file.filename) # filenames can be dangerous!
- file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
- return make_response("Success", 201)
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement