28

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
        else:
            # return error
            return {'False'}

Route

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!

Sigils
  • 2,492
  • 8
  • 24
  • 36
  • try using Blue Imp jQuery File Upload https://github.com/blueimp/jQuery-File-Upload – iJade Mar 11 '15 at 09:41
  • Hello @iJade , I am using Angular as front-end tho! But thanks for the recommendation! I only need to know how you do this at the server side! :) – Sigils Mar 11 '15 at 09:43

6 Answers6

28

The following is enough to save the uploaded file:

from flask import Flask
from flask_restful import Resource, Api, reqparse
import werkzeug

class 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")
Rob
  • 3,333
  • 5
  • 28
  • 71
Ron Harlev
  • 16,227
  • 24
  • 89
  • 132
  • What's the difference between `werkzeug.datastructures.FileStorage` and `werkzeug.FileStorage` ? – DarkSuniuM Oct 27 '18 at 18:29
  • 3
    It should be mentioned how that API can be used: Here is a `curl` example: `curl -v -X POST -H "Content-Type: multipart/form-data" -F "file=@test.zip" http://localhost:8080/api/maintenance/update` Using application/octet-stream as content type did not work out for me... – domih Nov 07 '18 at 09:48
  • Skipping `-H "Content-Type: multipart/form-data"` worked for me – daparic Oct 02 '19 at 18:00
23
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

Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55
6

you can use the request from flask

class UploadImage(Resource):
    def post(self, fname):
        file = request.files['file']
        if file and allowed_file(file.filename):
            # From flask uploading tutorial
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
        else:
            # return error
            return {'False'}

http://flask.pocoo.org/docs/0.12/patterns/fileuploads/

Jethro Guce
  • 69
  • 1
  • 1
4

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})
iJade
  • 23,144
  • 56
  • 154
  • 243
4

Following is a simple program to upload an image using curl and saving it locally.

from flask import Flask
from flask_restful import Resource, Api, reqparse
import werkzeug
import cv2
import numpy as np

app = Flask(__name__)
api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument('file',
                    type=werkzeug.datastructures.FileStorage,
                    location='files',
                    required=True,
                    help='provide a file')

class SaveImage(Resource):
    def post(self):
        args = parser.parse_args()
        # read like a stream
        stream = args['file'].read()
        # convert to numpy array
        npimg = np.fromstring(stream, np.uint8)
        # convert numpy array to image
        img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
        cv2.imwrite("saved_file.jpg", img)

api.add_resource(SaveImage, '/image')

if __name__ == '__main__':
    app.run(debug=True)

You can like using curl like this:

curl localhost:port/image -F file=@image_filename.jpg

Reference

0
#Image(s) and file Upload
from flask import Flask, json, request, jsonify
import os
import urllib.request
from werkzeug.utils import secure_filename

app = Flask(__name__)

app.secret_key = "caircocoders-ednalan"

UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/api/upload', methods=['POST'])
def upload_image():
    if 'image' not in request.files:
        resp = jsonify({
            'status' : False,
            'message' : 'Image is not defined'})
        resp.status_code = 400
        return resp

    files = request.files.getlist('image')

    errors = {}
    success = False

for photo in files:

    if photo and allowed_file(photo.filename):
        filename = secure_filename(photo.filename)
        photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        success = True
    else:
        errors[photo.filename] = 'Image type is not allowed'

if success and errors:
    errors['message'] = jsonify({
        'data' : photo.filename,
        'status' : True,
        'message' : 'Image(s) successfully uploaded'})
    resp = jsonify(errors)
    resp.status_code = 500
    return resp

if success:
    resp = jsonify({
        'data' : photo.filename,
        'status' : True,
        'message' : 'Images successfully uploaded'})
    resp.status_code = 201
    return resp
else:
    resp = jsonify(errors)
    resp.status_code = 500
    return resp