Skip to content

Instantly share code, notes, and snippets.

@kenwoodjw
Created April 10, 2018 03:04
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kenwoodjw/5dc7d8289e150e51567005095e4af12d to your computer and use it in GitHub Desktop.
Save kenwoodjw/5dc7d8289e150e51567005095e4af12d to your computer and use it in GitHub Desktop.
flask upload image return image base64
#coding:utf-8
import os,io
from flask import Flask, request, redirect, url_for,Response,render_template,send_file,make_response,jsonify,send_from_directory
from werkzeug import secure_filename
from flask_cors import CORS,cross_origin
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static\\upload')
VIDEO_FOLDER = os.path.join(APP_ROOT,'static\\video')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
cors = CORS(app, allow_headers='Content-Type', CORS_SEND_WILDCARD=True)
def allowed_file(filename):
"""
:param filename:
:return:
"""
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET','POST'])
@cross_origin(origins='*', send_wildcard=True)
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
byte_io = io.BytesIO()
byte_io.write(file.read())
byte_io.seek(0)
response = make_response(send_file(byte_io,mimetype='image/jpg'))
response.headers['Content-Transfer-Encoding']='base64'
return response
return render_template('upload.html')
if __name__ == '__main__':
app.run('0.0.0.0')
@m0ckingbird23
Copy link

Hey,
where can i find the related html files of this code??

@BlackFoxgamingstudio
Copy link

<!doctype html> <title>Upload new File</title> <h1>Upload new File</h1> <form method=post enctype=multipart/form-data> <input type=file name=file> <input type=submit value=Upload> </form>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment