Flask-RESTful - Upload image
The following is enough to save the uploaded file:
from flask import Flaskfrom flask_restful import Resource, Api, reqparseimport werkzeugclass UploadImage(Resource): def post(self): parse = reqparse.RequestParser() parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files') args = parse.parse_args() image_file = args['file'] image_file.save("your_file_name.jpg")
class UploadWavAPI(Resource): def post(self): parse = reqparse.RequestParser() parse.add_argument('audio', type=werkzeug.FileStorage, location='files') args = parse.parse_args() stream = args['audio'].stream wav_file = wave.open(stream, 'rb') signal = wav_file.readframes(-1) signal = np.fromstring(signal, 'Int16') fs = wav_file.getframerate() wav_file.close()
You should process the stream, if it was a wav, the code above works.For an image, you should store on the database or upload to AWS S3 or Google Storage
Something on the lines of the following code should help.
@app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST': file = request.files['file'] extension = os.path.splitext(file.filename)[1] f_name = str(uuid.uuid4()) + extension file.save(os.path.join(app.config['UPLOAD_FOLDER'], f_name)) return json.dumps({'filename':f_name})