flask和富文本编辑wangeditor的结合上传图片到本地

wangeditor参考文档:https://www.kancloud.cn/wangfupeng/wangeditor2/113961

上传html页面

 <div class="aui-form-group clear">
                    <label class="aui-label-control">
                     内容 <em>*</em>
                    </label>
                    <div id="editor" class="aui-form-input">
                    </div>
 </div>
 <input type="hidden" name="contents" value="" id="cont">

上传js

   <script type="text/javascript" src="../static/js/blogging/wangEditor.min.js"></script>
    <script type="text/javascript">
        var E=window.wangEditor
        var editor=new E('#editor')
        //编辑器中的图片上传到本地
        editor.customConfig.customUploadImg = function (files, insert) {
            var formData=new FormData();
            formData.append('f',files[0]);
            $.ajax({
                type:'POST',
                url:"{{url_for('upload')}}",
                data:formData,
                contentType:false,
                processData:false,
                success:function(data){
                //将返回的本地地址插入到wangeditor中
                    insert(data)
                }

            })
        }

        // 时刻监控编辑器,将数据传递到隐藏域input
        var cont=$('#cont')
        editor.customConfig.onchange=function(html){
           cont.val(html)
        }
        editor.create()
        // 初始化input值
        cont.val(editor.txt.html())
        
    </script>

flask处理响应

UP_LOAD='app/static/upload/image'
SAVE_PATH='../static/upload/image'
DIR=os.path.join(os.getcwd(),UP_LOAD)


@app.route('/upload',methods=['POST'])
def upload():
    fobject=request.files.get('f')
    img_path=os.path.join(DIR,fobject.filename)
    fobject.save(img_path)
    save_path=os.path.join(SAVE_PATH,fobject.filename)
    return save_path

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值