20,359
社区成员
发帖
与我相关
我的任务
分享
//上传
public function uploadJson(){
$dir = I('get.dir');
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
// 设置附件上传类型
switch ($dir){
case 'image':
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');
break;
case 'file':
$upload->exts = array('pptx','ppt','docx','doc','txt','dotx','xlsx','xls','jpg','png');
break;
default:
$this->ajaxReturn(array('error' => 1, 'message' => '参数错误'));
}
$upload->rootPath = './Uploads/'; // 设置附件上传根目录
$upload->savePath = $dir.'/'; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
$img_url = $upload->rootPath.$info['imgFile']['savepath'].$info['imgFile']['savename'];
if(!$info) {
// 上传错误提示错误信息
$this->ajaxReturn(array('error' => 1, 'message' => $upload->getError()));
}else{
// 上传成功
$downloadedfile = array('viewhost'=>'/Uploads/', 'filename'=>$info['imgFile']['name'], 'filepath'=>$img_url, 'filesize'=>$info['imgFile']['size'], 'fileext'=>$info['imgFile']['ext'], 'authcode' => $info['imgFile']['md5'], 'savepath'=>$info['imgFile']['savepath'], 'savename'=>$info['imgFile']['savename']);
D('Attachment')->saveData($downloadedfile, $dir);
$this->ajaxReturn(json_encode(array('error' => 0, 'url' => $img_url, 'authcode'=>$info['imgFile']['md5'])), 'EVAL');
}
}
var fileUploader = KindEditor.editor({
allowFileManager : true,
uploadJson : '{:U('Attachment/uploadJson')}', //更改默认的上传控件
fileManagerJson : '{:U('Attachment/fileManagerJson')}', //更改默认的空间控件
});
KindEditor('#a_insertfile').click(function() {
fileUploader.loadPlugin('insertfile', function() {
fileUploader.plugin.fileDialog({
fileUrl : KindEditor('#a_attachment').val(),
clickFn : function(url, title) {
KindEditor('#a_attachment').val(url);
$('#a_attachment_authcode').val(authcode);
$('#a_attachment_clear').show();
fileUploader.hideDialog();
}
});
});
});