Last active
May 15, 2019 03:24
-
-
Save renmu123/027390a7c9852891b356ea64c6073d9d to your computer and use it in GitHub Desktop.
Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask_restful import Resource, Api, reqparse | |
import werkzeug, os | |
app = Flask(__name__) | |
api = Api(app) | |
UPLOAD_FOLDER = 'static/img' | |
parser = reqparse.RequestParser() | |
parser.add_argument('file',type=werkzeug.datastructures.FileStorage, location='files') | |
class HelloWorld(Resource): | |
def get(self): | |
return {'hello': 'world'} | |
class PhotoUpload(Resource): | |
decorators=[] | |
def post(self): | |
data = parser.parse_args() | |
if data['file'] == "": | |
return { | |
'data':'', | |
'message':'No file found', | |
'status':'error' | |
} | |
photo = data['file'] | |
if photo: | |
filename = 'your_image.png' | |
photo.save(os.path.join(UPLOAD_FOLDER,filename)) | |
return { | |
'data':'', | |
'message':'photo uploaded', | |
'status':'success' | |
} | |
return { | |
'data':'', | |
'message':'Something when wrong', | |
'status':'error' | |
} | |
api.add_resource(HelloWorld, '/') | |
api.add_resource(PhotoUpload,'/upload') | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment