Flask-RESTful - Download Picture

I was wondering how you upload files while 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 HTML

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

I have enabled server side CORS

I use angular.js as front-end and ng-upload if that matters, but can also use CURL instructions!

+10
source share
5 answers
 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 must process the stream if it was wav, the code above works. For an image, you must store in a database or upload to AWS S3 or Google Storage

+13
source

Enough to save the downloaded file

  from flask import Flask from flask_restful import Resource, Api, reqparse import werkzeug class UploadAudio(Resource): def post(self): parse = reqparse.RequestParser() parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files') args = parse.parse_args() audioFile = args['file'] audioFile.save("your_file_name.jpg") 
+8
source

Something in 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}) 
+6
source

you can use a query from a 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/

+3
source
 from flask import Flask, url_for, send_from_directory, request import logging, os from werkzeug import secure_filename app = Flask(__name__) file_handler = logging.FileHandler('server.log') app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) PROJECT_HOME = os.path.dirname(os.path.realpath(__file__)) UPLOAD_FOLDER = '{}/uploads/'.format(PROJECT_HOME) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER def create_new_folder(local_dir): newpath = local_dir if not os.path.exists(newpath): os.makedirs(newpath) return newpath @app.route('/', methods = ['POST']) def api_root(): app.logger.info(PROJECT_HOME) if request.method == 'POST' and request.files['image']: app.logger.info(app.config['UPLOAD_FOLDER']) img = request.files['image'] img_name = secure_filename(img.filename) create_new_folder(app.config['UPLOAD_FOLDER']) saved_path = os.path.join(app.config['UPLOAD_FOLDER'], img_name) app.logger.info("saving {}".format(saved_path)) img.save(saved_path) return send_from_directory(app.config['UPLOAD_FOLDER'],img_name, as_attachment=True) else: return "Where is the image?" if __name__ == '__main__': app.run(host='0.0.0.0', debug=False) 

Here is the link

0
source

Source: https://habr.com/ru/post/983637/


All Articles