TinyMCE的上传文件的功能

记录一下TinyMCE的上传文件的功能:

用Base64上传图片:

if (meta.filetype == 'image') {
        var input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.setAttribute('accept', 'image/*');

        /*
        Note: In modern browsers input[type="file"] is functional without
        even adding it to the DOM, but that might not be the case in some older
        or quirky browsers like IE, so you might want to add it to the DOM
        just in case, and visually hide it. And do not forget do remove it
        once you do not need it anymore.
        */

        input.onchange = function () {
        var file = this.files[0];

        var reader = new FileReader();
        reader.onload = function () {
            /*
            Note: Now we need to register the blob in TinyMCEs image blob
            registry. In the next release this part hopefully won't be
            necessary, as we are looking to handle it internally.
            */
            var id = 'blobid' + (new Date()).getTime();
            var blobCache = tinymce.activeEditor.editorUpload.blobCache;
            var base64 = reader.result.split(',')[1];
            var blobInfo = blobCache.create(id, file, base64);
            blobCache.add(blobInfo);

            /* call the callback and populate the Title field with the file name */
            cb(blobInfo.blobUri(), { title: file.name });
        };
        reader.readAsDataURL(file);
        };

        input.click();
    }

用XMLHttp上传视频:

if (meta.filetype == 'media') {
        //创建一个隐藏的type=file的文件选择input
        let input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.onchange = function () {
        let file = this.files[0];//只选取第一个文件。如果要选取全部,后面注意做修改
        let xhr, formData;
        xhr = new XMLHttpRequest();
        xhr.open('POST', '/api/district/video/upload');
        xhr.withCredentials = true;
        xhr.upload.onprogress = function (e) {
            // 进度(e.loaded / e.total * 100)
            debugger
        };
        xhr.onerror = function () {
            //根据自己的需要添加代码
            console.log(xhr.status);
            return;
        };
        xhr.onload = function () {
            let json;
            if (xhr.status < 200 || xhr.status >= 300) {
            console.log('HTTP 错误: ' + xhr.status);
            return;
            }
            json = JSON.parse(xhr.responseText);
            //假设接口返回JSON数据为{status: 0, msg: "上传成功", data: {location: "/localImgs/1546434503854.mp4"}}
            if (json.status == 0) {
            //接口返回的文件保存地址
            let mediaLocation = json.data.location;
            //cb()回调函数,将mediaLocation显示在弹框输入框中
            cb(mediaLocation, { title: file.name });
            } else {
            console.log(json.msg);
            return;
            }

        };
        formData = new FormData();
        //假设接口接收参数为file,值为选中的文件
        formData.append('file', file);
        //正式使用将下面被注释的内容恢复
        xhr.send(formData);
        }
        //触发点击
        input.click();
    }

使用swagger+openapi的接口:

    if (meta.filetype == 'image') {
        //创建一个隐藏的type=file的文件选择input
        let input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.onchange = function () {
            let file = this.files[0];//只选取第一个文件。如果要选取全部,后面注意做修改
            // console.log('file', file)
            dynamicController.uploadPicFileUsingPOST({ name: file.name }, { file: file }).then(res => {
                // console.log('res:', res)
                cb(res.data?.path, { title: file.name })
            })
        }
        //触发点击
        input.click();
    }else if(meta.filetype == 'media'){
        let input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.onchange = function () {
            let file = this.files[0];//只选取第一个文件。如果要选取全部,后面注意做修改
            // console.log('file', file)
            dynamicController.uploadVideoFileUsingPOST({ name: file.name }, { file: file }).then(res => {
                // console.log('res:', res)
                cb(res.data?.path, { title: file.name })
            })
        }
        //触发点击
        input.click();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值