|
(function($) {
$.widget("mxt.uploadFile",{
_create:function(){
var $self = this.element,self = this;
$self.click(function(){
api.getPicture({sourceType: 'library',
encodingType: 'png',
mediaValue: 'pic',
destinationType: 'url',
allowEdit: true,
quality: 50,
targetWidth:100,
targetHeight:100,
saveToPhotoAlbum: false
},function(ret,err){
self.options = $.extend(self.options,{files:ret.data});
if(ret){
if(ret.data){
self._fileUpload();
}
}
});
});
},
_fileUpload:function(){
var options = this.options;
//开始上传回调事件
if(typeof options.onUploadStar == "function"){
options.onUploadStar();
}
if(localStorage.getItem("userlogin")){
var user = JSON.parse(localStorage.getItem("user"));
options.data.mxauth = user.mxauth;
}
//上传方法
api.ajax({
urlptions.url,
method: 'post',
cache: false,
timeout: 30,
dataType: 'json',
returnAll:false,
report:true,
data:{
valuesptions.data,//上传数据
files: {file: options.files}//文件路径
}
},function(ret,err){
if (ret) {
if(ret.progress){
if(typeof options.uploadProgress == "function"){
options.uploadProgress(ret.progress);
}
}
if(ret.progress == 100){
setTimeout(function(){
if(typeof options.uploadComplete == "function"){
options.uploadComplete(ret.body);
}
},1000);
}
}else {
//失败回调方法
if(typeof options.onUploadError == "function"){
options.onUploadError(err.code,err.msg,err);
}
};
});
}
});
})($); |
|