Python+Flask(3)测试使用ueditor实现富文本图片上传

先看最终实现效果图:

Python+Flask(3)测试使用ueditor实现富文本图片上传_第1张图片

Python+Flask(3)测试使用ueditor实现富文本图片上传_第2张图片Python+Flask(3)测试使用ueditor实现富文本图片上传_第3张图片 

 实现步骤如下:

1.网上下载ueditor

        下载地址:http://ueditor.baidu.com/website/download.html#ueditor​

2.项目目录结构如下:

Python+Flask(3)测试使用ueditor实现富文本图片上传_第4张图片

 3.重点文件代码:

         1)app.py

from flask import Flask,render_template
from ueditor import bp
import config
app = Flask(__name__)
app.config.from_object(config)
app.register_blueprint(bp)

@app.route('/')
def index():
    return render_template('index.html')

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

2)config.py

import os

#上传到本地
UEDITOR_UPLOAD_PATH = os.path.join(os.path.dirname(__file__), 'static\\images')

3)ueditor.py

#encoding: utf-8
from flask import Blueprint,request,jsonify,url_for,send_from_directory,current_app as app
from flask_wtf import CSRFProtect
import json
import re
import string
import time
import hashlib
import random
import base64
import sys
import os
from urllib import parse

os.chdir(os.path.abspath(sys.path[0]))
from io import BytesIO

bp = Blueprint('ueditor',__name__,url_prefix='/ueditor')

UEDITOR_UPLOAD_PATH = ""

@bp.before_app_first_request
def before_first_request():
    global UEDITOR_UPLOAD_PATH

    UEDITOR_UPLOAD_PATH = app.config.get('UEDITOR_UPLOAD_PATH')
    if UEDITOR_UPLOAD_PATH and not os.path.exists(UEDITOR_UPLOAD_PATH):
        os.mkdir(UEDITOR_UPLOAD_PATH)

def _random_filename(rawfilename):
    letters = string.ascii_letters
    random_filename = str(time.time()) + "".join(random.sample(letters,5))
    filename = hashlib.md5(random_filename.encode('utf-8')).hexdigest()
    subffix = os.path.splitext(rawfilename)[-1]
    return filename + subffix

@bp.route('/upload/',methods=['GET','POST'])
def upload():
    action = request.args.get('action')
    result = {}
    if action == 'config':
        config_path = os.path.join(bp.static_folder or app.static_folder,'ueditor','config.json')

        with open(config_path,'r',encoding='utf-8') as fp:
            result = json.loads(re.sub(r'\/\*.*\*\/','',fp.read()))

    elif action in ['uploadimage','uploadvideo','uploadfile']:
        image = request.files.get("upfile")
        filename = image.filename
        save_filename = _random_filename(filename)
        result = {
            'state': '',
            'url': '',
            'title': '',
            'original': ''
        }
        image.save(os.path.join(UEDITOR_UPLOAD_PATH, save_filename))
        result['state'] = "SUCCESS"
        result['url'] = url_for('ueditor.files', filename=save_filename)
        result['title'] = save_filename
        result['original'] = save_filename

    elif action == 'uploadscrawl':#执行上传涂鸦的action名称
        base64data = request.form.get("upfile")
        img = base64.b64decode(base64data)
        filename = _random_filename('xx.png')
        filepath = os.path.join(UEDITOR_UPLOAD_PATH,filename)
        with open(filepath,'wb') as fp:
            fp.write(img)
        result = {
            "state": "SUCCESS",
            "url": url_for('files',filename=filename),
            "title": filename,
            "original": filename
        }

    return jsonify(result)

@bp.route('/files//')
def files(filename):
    return send_from_directory(UEDITOR_UPLOAD_PATH,filename)

4)templates\index.html




    
    
    
    
    Ewangda--测试ueditor






你可能感兴趣的:(python,flask,python,flask,后端)