Dear Moderator,
I am trying to learn Rest framework through Restful Flask.
My initial exercises went fine with https://flask-restful.readthedocs.org/en/0.3.3/quickstart.html
Now I want to upload file through Restful Flask. I tried to check the web for reference.
I got these urls,
(i) http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
(ii) http://stackoverflow.com/questions/28982974/flask-restful-upload-image
(iii) http://blog.luisrei.com/articles/flaskrest.html
But the question I am stuck with what are the things I have to change in the example of quickstart tutorial so that I may be able to upload file. Or if any one may kindly suggest with a small example.
If any one of the esteemed members may kindly suggest.
Regards,
Subhabrata Banerjee.
Hello,
I've the same concern as Subhabrata ; I just want to upload a file by using Flask-Rest... any example?
regards
Please check the docs.
@joshfriend The link is restricted to visit.
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)
Make sure that the [UPLOAD_FOLDER] was created or it will throw an error
Adding a sample curl client use case that works: curl -X POST -F file=@'/tmp/sweetmp.png' http://192.168.3.50:5000/upload
Most helpful comment
Make sure that the [UPLOAD_FOLDER] was created or it will throw an error