Flask-RESTful – Upload image


I was wondering on how do you upload files by creating an API service?

class UploadImage(Resource):
    def post(self, fname):
        file = request.files['file']
        if file:
            # save image
            # return error
            return {'False'}


api.add_resource(UploadImage, '/api/uploadimage/<string:fname>')

And then the HTML

   <input type="file" name="file">

I have enabled CORS on the server side

I am using angular.js as front-end and ng-upload if that matters, but can use CURL statements too!


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()

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

Answered By – Sibelius Seraphini

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published