/*
又拍云插件--Power by Andiff weixin:andiff424 QQ:165607361
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node / CommonJS
factory(require('jquery'));
} else {
// Browser globals.
factory(jQuery);
}
})(function ($) {
'use strict';
//常量
var UPYUNURL = 'http://v0.api.upyun.com/cunzhang';
//上传接口地址
var BUCKET = '你的BUCKET';
//Bucket
var FORMAPI = '你的FORMAPI';
//formapi
var SAVEPATH = '服务器保存的文件夹';
//请求的地址
var URL = '/addons/upyun/index/rentoupyun.html?savepath=';
var upyun = {
//类似朋友圈上传多张图片
uploadPicsByAjax:function(index,ul,dir){
$.getJSON(URL+dir, function (data) {
var uploadData = new FormData();
uploadData.append('file', $('#files'+index)[0].files[0]);
uploadData.append('policy', data.policy);
uploadData.append('authorization', data.authorization);
$.ajax({
url: UPYUNURL,
type: 'POST',
data: uploadData,
cache: false,
processData: false,
contentType: false,
}).done(function(data, textStatus) {
//alert(JSON.stringify(data));
var body = jQuery.parseJSON(data);
var img = '<img width="100%" src="http://cz.andiff.wang/'+body.url+'" />';
img = img + '<img class="imgcommon" src="/images/cha.png" />';
img = img + '<input type="hidden" name="row[picurl]" value="'+body.url+'" />';
$("#"+ul+" li").eq(index).html(img);
}).fail(function(res, textStatus, error) {
try {
var body = JSON.parse(res.responseText);
alert('error: ' + body.message);
} catch(e) {
console.error(e);
}
});
});
},
//普通的上传文件
uploadByAjax: function (file1,show1,dir) {
$.getJSON(URL+dir, function (data) {
var uploadData = new FormData();
uploadData.append('file', $('#'+file1)[0].files[0]);
uploadData.append('policy', data.policy);
uploadData.append('authorization', data.authorization);
$.ajax({
url: UPYUNURL,
type: 'POST',
data: uploadData,
cache: false,
processData: false,
contentType: false,
}).done(function(data, textStatus) {
//alert(JSON.stringify(data));
var body = jQuery.parseJSON(data);
$("#"+show1).val(body.url);
}).fail(function(res, textStatus, error) {
try {
var body = JSON.parse(res.responseText);
alert('error: ' + body.message);
} catch(e) {
console.error(e);
}
});
});
},
//summernote文字编辑器里面上传图片
uploadForSummerNote: function (file1,show1) {
$.getJSON(URL+'summernote', function (data) {
var uploadData = new FormData();
uploadData.append("file", file1);
uploadData.append('policy', data.policy);
uploadData.append('authorization', data.authorization);
$.ajax({
url: UPYUNURL,
//url: '/index/rentoupyun',
type: 'POST',
data: uploadData,
cache: false,
processData: false,
contentType: false,
}).done(function(data, textStatus) {
var body = jQuery.parseJSON(data);
$("#"+show1).summernote('insertImage', 'http://cz.andiff.wang/'+body.url, 'image name');
}).fail(function(res, textStatus, error) {
try {
var body = JSON.parse(res.responseText);
alert('error: ' + body.message);
} catch(e) {
console.error(e);
}
});
});
//return $.inArray(type, this.dems) !== -1;
},
};
return upyun;
});
引用示例:
$("#upyun_upload").bind('change',function(){
UpYun.uploadByAjax('upyun_upload','c-logo','store');
});
如何绑定Summernote
$('#c-intro').summernote({
height: 150, //set editable area's height
codemirror: { // codemirror options
theme: 'monokai'
},
callbacks: {
onImageUpload: function(files) { //the onImageUpload API
//img = sendFile(files[0]);
UpYun.uploadForSummerNote(files[0],'c-intro');
}
}
});