Advertisement
Guest User

Untitled

a guest
Jul 5th, 2019
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import os
  2. from flask import Flask, jsonify, request, make_response
  3. from flask_cors import CORS
  4. from werkzeug.utils import secure_filename
  5.  
  6. UPLOAD_FOLDER='/images'
  7. ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  8.  
  9. app = Flask(__name__)
  10. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  11. CORS(app)
  12.  
  13.  
  14. def allowed_file(filename):
  15.     return '.' in filename and \
  16.         filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  17.  
  18. @app.route('/', methods=['GET', 'POST'])
  19. def upload_file():
  20.  
  21.     print(request.files)
  22.     if request.method == "POST":
  23.         # check if the post request has the file part
  24.         if 'file' not in request.files:
  25.             print('No file part')
  26.             return make_response("No File Part", 400)
  27.         file = request.files["file"]
  28.         # if user does not select file, browser also submit an empty part
  29.         # without filename
  30.         if file.filename == '':
  31.             print('No selected file')
  32.             return make_response("No Selected File", 400)
  33.         if file and allowed_file(file.filename):
  34.             filename = secure_filename(file.filename) # filenames can be dangerous!
  35.             file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  36.             return make_response("Success", 201)
  37.  
  38. if __name__ == '__main__':
  39.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement