notadd / neditor

基于 ueditor的更现代化的富文本编辑器,支持HTTPS
https://demo.neditor.notadd.com/
MIT License
1.9k stars 267 forks source link

自定义上传到 OSS 的插件setFormData不管用 #169

Closed baiyuxiong closed 4 years ago

baiyuxiong commented 4 years ago

通过修改/neditor/neditor.service.js来实现阿里云OSS 直传。 因为要上传前需要获取授权token,我在setFormData方法里通过XMLHttpRequest调用接口获取授权参数。代码如下:

setFormData: function(object, data, headers) {
        var upload_dir = editor.getOpt("ossDir");
        var key = upload_dir+"/" + new Date().getTime() + "." + object.blob.ext;
        data.key = key; //这个在上传的时候 http 请求体里有
        let url = editor.getOpt("ossSignUrl");
        if (url) {
            let xhr_sign  = new XMLHttpRequest();
            xhr_sign.withCredentials = false;
            xhr_sign.open('POST', url,true);
            xhr_sign.onload = function() {
                var json_sign;
                if (xhr_sign.status != 200) {
                    console.log('HTTP Error: ' + xhr_sign.status);
                    return false;
                }
                json_sign = JSON.parse(xhr_sign.responseText);
                if (! json_sign || json_sign.code != 200) {
                    console.log('Invalid JSON: ' + xhr_sign.responseText);
                    return false;
                }
                let signData = json_sign.data;
                data.policy = signData.policy;//以下几个参数,上传的时候 http 请求体里就没有
                data.OSSAccessKeyId = signData.accessid;
                data.success_action_status = 200;
                data.signature = signData.signature;
            };
            xhr_sign.send();
        }
        function sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms))
        }
        (async function() {
            await sleep(3000);
        })();
    console.log(data);//这个打印结果里有policy OSSAccessKeyId这些参数,
    return data;
},

疑问: 1、为什么最终打印的 data 里有授权返回的参数,但是上传请求的时候没有带授权参数? 2、 data.key这个参数不在XMLHttpRequest请求里,为什么就会带上? 3、该如何处理?

ekxs0109 commented 4 years ago

直接data.xxxx= xxx;header.token = 'xxxxx';

可以直接修改对象。里面的数据是引用的