summernote 富文本编辑器上传七牛云服务器

首先 js 上传到七牛云 需要两个参数 一个是 url  一个是 token

引入 js

summernote.min.js
summernote-zh-CN.js
qiniu.min.js
    
    
	

完整 HTML








    

 

 

关键代码 七牛云参数从服务器获取

        var uploadPath = ctx + "api/deal/getQiniu";
        // 富文本编辑器:1.引入4个summernote文件(2个css和2个js)
        // 富文本编辑器:2.js初始化
        // 富文本编辑器:3.重写图片(七牛云)上传
        function sendFile(file, editor, welEditable){
            var layerIndex = layer.load(0);
            $.ajax({
                url: uploadPath, success: function (res) {
//                    console.log(res.data);
                    var data = res.data;
                    //七牛云上传 token
                    var token = data.qiniu;
                    // 七牛云上传地址
                    var domain = data.url;
                    var config = {
                        useCdnDomain: true,
                        disableStatisticsReport: false,
                        retryCount: 6,
                        region: null
                    };
                    var putExtra = {
                        fname: "",
                        params: {},
                        mimeType: null
                    };
                    // 设置next,error,complete对应的操作,分别处理相应的进度信息,错误信息,以及完成后的操作
                    var error = function (err) {
                        console.log(err);
                        layer.close(layerIndex);
                        layer.alert('上传失败!');
                        return;
                    };
                    var complete = function (res) {
                        var imageUrl = domain + res.key;
                        editor.insertImage(welEditable, imageUrl);
                        layer.close(layerIndex);
                    };
                    var next = function (response) {
                        console.log(response);
                    };
                    var subObject = {
                        next: next,
                        error: error,
                        complete: complete
                    };
                    // 调用sdk上传接口获得相应的observable,控制上传和暂停
                    observable = qiniu.upload(file, null, token, putExtra, config);
                    observable.subscribe(subObject);
                }
            });
        }
        $(function() {
            $('.summernote').summernote({
                height : '220px',
                lang : 'zh-CN',
                onImageUpload: function(files,editor,welEditable) {
                    sendFile(files[0],editor,welEditable);
                }
            });
            var content = $("#noticeContent").val();
            $('#editor').code(content);
        });

qiniu.min.js

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.qiniu=e():t.qiniu=e()}("undefined"!=typeof self?self:this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="/dist/",e(e.s=58)}([function(t,e){var n=t.exports="undefined"!=typeof window&&window.Math==Math?window:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();"number"==typeof __g&&(__g=n)},function(t,e){var n=t.exports={version:"2.5.7"};"number"==typeof __e&&(__e=n)},function(t,e,n){var r=n(30)("wks"),o=n(20),i=n(0).Symbol,u="function"==typeof i;(t.exports=function(t){return r[t]||(r[t]=u&&i[t]||(u?i:o)("Symbol."+t))}).store=r},function(t,e,n){var r=n(7);t.exports=function(t){if(!r(t))throw TypeError(t+" is not an object!");return t}},function(t,e,n){var r=n(0),o=n(1),i=n(17),u=n(5),s=n(9),a=function(t,e,n){var c,f,l,p=t&a.F,h=t&a.G,d=t&a.S,v=t&a.P,y=t&a.B,m=t&a.W,g=h?o:o[e]||(o[e]={}),b=g.prototype,_=h?r:d?r[e]:(r[e]||{}).prototype;for(c in h&&(n=e),n)(f=!p&&_&&void 0!==_[c])&&s(g,c)||(l=f?_[c]:n[c],g[c]=h&&"function"!=typeof _[c]?n[c]:y&&f?i(l,r):m&&_[c]==l?function(t){var e=function(e,n,r){if(this instanceof t){switch(arguments.length){case 0:return new t;case 1:return new t(e);case 2:return new t(e,n)}return new t(e,n,r)}return t.apply(this,arguments)};return e.prototype=t.prototype,e}(l):v&&"function"==typeof l?i(Function.call,l):l,v&&((g.virtual||(g.virtual={}))[c]=l,t&a.R&&b&&!b[c]&&u(b,c,l)))};a.F=1,a.G=2,a.S=4,a.P=8,a.B=16,a.W=32,a.U=64,a.R=128,t.exports=a},function(t,e,n){var r=n(6),o=n(19);t.exports=n(8)?function(t,e,n){return r.f(t,e,o(1,n))}:function(t,e,n){return t[e]=n,t}},function(t,e,n){var r=n(3),o=n(41),i=n(28),u=Object.defineProperty;e.f=n(8)?Object.defineProperty:function(t,e,n){if(r(t),e=i(e,!0),r(n),o)try{return u(t,e,n)}catch(t){}if("get"in n||"set"in n)throw TypeError("Accessors not supported!");return"value"in n&&(t[e]=n.value),t}},function(t,e){t.exports=function(t){return"object"==typeof t?null!==t:"function"==typeof t}},function(t,e,n){t.exports=!n(10)(function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a})},function(t,e){var n={}.hasOwnProperty;t.exports=function(t,e){return n.call(t,e)}},function(t,e){t.exports=function(t){try{return!!t()}catch(t){return!0}}},function(t,e,n){var r=n(45),o=n(26);t.exports=function(t){return r(o(t))}},function(t,e){t.exports=!0},function(t,e){t.exports={}},function(t,e,n){var r=n(44),o=n(31);t.exports=Object.keys||function(t){return r(t,o)}},function(t,e){var n={}.toString;t.exports=function(t){return n.call(t).slice(8,-1)}},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function o(t){return(0,d.default)(t).filter(function(t){return t.startsWith("x:")}).map(function(e){return[e,t[e].toString()]})}function i(t){return"qiniu_js_sdk_upload_file_"+t.name+"_size_"+t.size}function u(t){return{Authorization:"UpToken "+t}}function s(){return window.XMLHttpRequest?new XMLHttpRequest:new window.ActiveXObject("Microsoft.XMLHTTP")}function a(t){return new l.default(function(e,n){var r=new FileReader;r.readAsArrayBuffer(t),r.onload=function(t){var n=t.target.result;e(n)},r.onerror=function(){n(new Error("fileReader 读取错误"))}})}function c(t,e){return new l.default(function(n,r){var o=s();o.open(e.method,t),e.onCreate&&e.onCreate(o),e.headers&&(0,d.default)(e.headers).forEach(function(t){return o.setRequestHeader(t,e.headers[t])}),o.upload.addEventListener("progress",function(t){t.lengthComputable&&e.onProgress&&e.onProgress({loaded:t.loaded,total:t.total})}),o.onreadystatechange=function(){var t=o.responseText;if(4===o.readyState){var e=o.getResponseHeader("x-reqId")||"";if(200!==o.status){var i="xhr request failed, code: "+o.status+";";return t&&(i=i+" response: "+t),void r({code:o.status,message:i,reqId:e,isRequestError:!0})}try{n({data:JSON.parse(t),reqId:e})}catch(t){r(t)}}},o.send(e.body)})}function f(){return"http:"===window.location.protocol?"http:":"https:"}e.__esModule=!0;var l=r(n(24)),p=r(n(54)),h=r(n(86)),d=r(n(55));e.isChunkExpired=function(t){var e=t+864e5;return(new Date).getTime()>e},e.getChunks=function(t,e){for(var n=[],r=Math.ceil(t.size/e),o=0;o-1};var v=n(56),y=n(37),m=r(n(91))},function(t,e,n){var r=n(18);t.exports=function(t,e,n){if(r(t),void 0===e)return t;switch(n){case 1:return function(n){return t.call(e,n)};case 2:return function(n,r){return t.call(e,n,r)};case 3:return function(n,r,o){return t.call(e,n,r,o)}}return function(){return t.apply(e,arguments)}}},function(t,e){t.exports=function(t){if("function"!=typeof t)throw TypeError(t+" is not a function!");return t}},function(t,e){t.exports=function(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}}},function(t,e){var n=0,r=Math.random();t.exports=function(t){return"Symbol(".concat(void 0===t?"":t,")_",(++n+r).toString(36))}},function(t,e,n){var r=n(6).f,o=n(9),i=n(2)("toStringTag");t.exports=function(t,e,n){t&&!o(t=n?t:t.prototype,i)&&r(t,i,{configurable:!0,value:e})}},function(t,e){e.f={}.propertyIsEnumerable},function(t,e,n){"use strict";e.__esModule=!0,e.default=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}},function(t,e,n){t.exports={default:n(59),__esModule:!0}},function(t,e){var n=Math.ceil,r=Math.floor;t.exports=function(t){return isNaN(t=+t)?0:(t>0?r:n)(t)}},function(t,e){t.exports=function(t){if(void 0==t)throw TypeError("Can't call method on  "+t);return t}},function(t,e,n){var r=n(7),o=n(0).document,i=r(o)&&r(o.createElement);t.exports=function(t){return i?o.createElement(t):{}}},function(t,e,n){var r=n(7);t.exports=function(t,e){if(!r(t))return t;var n,o;if(e&&"function"==typeof(n=t.toString)&&!r(o=n.call(t)))return o;if("function"==typeof(n=t.valueOf)&&!r(o=n.call(t)))return o;if(!e&&"function"==typeof(n=t.toString)&&!r(o=n.call(t)))return o;throw TypeError("Can't convert object to primitive value")}},function(t,e,n){var r=n(30)("keys"),o=n(20);t.exports=function(t){return r[t]||(r[t]=o(t))}},function(t,e,n){var r=n(1),o=n(0),i=o["__core-js_shared__"]||(o["__core-js_shared__"]={});(t.exports=function(t,e){return i[t]||(i[t]=void 0!==e?e:{})})("versions",[]).push({version:r.version,mode:n(12)?"pure":"global",copyright:"© 2018 Denis Pushkarev (zloirock.ru)"})},function(t,e){t.exports="constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf".split(",")},function(t,e,n){var r=n(26);t.exports=function(t){return Object(r(t))}},function(t,e,n){"use strict";var r=n(18);t.exports.f=function(t){return new function(t){var e,n;this.promise=new t(function(t,r){if(void 0!==e||void 0!==n)throw TypeError("Bad Promise constructor");e=t,n=r}),this.resolve=r(e),this.reject=r(n)}(t)}},function(t,e){e.f=Object.getOwnPropertySymbols},function(t,e,n){e.f=n(2)},function(t,e,n){var r=n(0),o=n(1),i=n(12),u=n(35),s=n(6).f;t.exports=function(t){var e=o.Symbol||(o.Symbol=i?{}:r.Symbol||{});"_"==t.charAt(0)||t in e||s(e,t,{value:u.f(t)})}},function(t,e,n){"use strict";e.__esModule=!0,e.regionUphostMap={z0:{srcUphost:"up.qiniup.com",cdnUphost:"upload.qiniup.com"},z1:{srcUphost:"up-z1.qiniup.com",cdnUphost:"upload-z1.qiniup.com"},z2:{srcUphost:"up-z2.qiniup.com",cdnUphost:"upload-z2.qiniup.com"},na0:{srcUphost:"up-na0.qiniup.com",cdnUphost:"upload-na0.qiniup.com"},as0:{srcUphost:"up-as0.qiniup.com",cdnUphost:"upload-as0.qiniup.com"}},e.region={z0:"z0",z1:"z1",z2:"z2",na0:"na0",as0:"as0"}},function(t,e){},function(t,e,n){"use strict";var r=n(60)(!0);n(40)(String,"String",function(t){this._t=String(t),this._i=0},function(){var t,e=this._t,n=this._i;return n>=e.length?{value:void 0,done:!0}:(t=r(e,n),this._i+=t.length,{value:t,done:!1})})},function(t,e,n){"use strict";var r=n(12),o=n(4),i=n(42),u=n(5),s=n(13),a=n(61),c=n(21),f=n(65),l=n(2)("iterator"),p=!([].keys&&"next"in[].keys()),h=function(){return this};t.exports=function(t,e,n,d,v,y,m){a(n,e,d);var g,b,_,x=function(t){if(!p&&t in C)return C[t];switch(t){case"keys":case"values":return function(){return new n(this,t)}}return function(){return new n(this,t)}},w=e+" Iterator",S="values"==v,k=!1,C=t.prototype,P=C[l]||C["@@iterator"]||v&&C[v],O=P||x(v),M=v?S?x("entries"):O:void 0,U="Array"==e&&C.entries||P;if(U&&(_=f(U.call(new t)))!==Object.prototype&&_.next&&(c(_,w,!0),r||"function"==typeof _[l]||u(_,l,h)),S&&P&&"values"!==P.name&&(k=!0,O=function(){return P.call(this)}),r&&!m||!p&&!k&&C[l]||u(C,l,O),s[e]=O,s[w]=h,v)if(g={values:S?O:x("values"),keys:y?O:x("keys"),entries:M},m)for(b in g)b in C||i(C,b,g[b]);else o(o.P+o.F*(p||k),e,g);return g}},function(t,e,n){t.exports=!n(8)&&!n(10)(function(){return 7!=Object.defineProperty(n(27)("div"),"a",{get:function(){return 7}}).a})},function(t,e,n){t.exports=n(5)},function(t,e,n){var r=n(3),o=n(62),i=n(31),u=n(29)("IE_PROTO"),s=function(){},a=function(){var t,e=n(27)("iframe"),r=i.length;for(e.style.display="none",n(47).appendChild(e),e.src="javascript:",(t=e.contentWindow.document).open(),t.write("
                    
                    

你可能感兴趣的:(js)