JS-wangEditor自定义上传多张图片

wangEditor是一款开源的富文本编辑器,除默认实现,还提供非常多自定义参数,看看如何自定义上传多张图片。

var E = window.wangEditor;
var editor =new E('#description');
initEditor(editor);

editor.customConfig.uploadImgServer = 'product/upload';
editor.customConfig.uploadImgHooks = {
    fail:function (xhr, editor, result) {
        //上传错误时触发
        alert("上传图片失败");
    }
};
editor.customConfig.customUploadImg = function(files, insert) {
    var formData = new FormData();
    for(var i = 0;i < files.length;i ++) {
        formData.append("files[" + i + "]", files[i], files[i].name);
    }
    $.ajax({
        url: rootPath + 'product/upload',
        type: "POST",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success:function(da){
            if(da.errno == 0){
                for(var j=0;j<da.data.length;j++){
                    insert(da.data[j]);
                }
            }else{
                alert(da.msg);
                return;
            }
        }
    });
};

// 将图片大小限制为 3M
editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;
// 限制一次最多上传 5 张图片
editor.customConfig.uploadImgMaxLength = 30;
// 或者 var editor = new E( document.getElementById('editor') )
editor.create();

通过自定义参数customUploadImg,重写上传图片逻辑,上传成功后返回图片地址,再添加到富文本编辑框中。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值