12-12 3,095 views
HTML 部分:
//引入jquery
<script src="/public/js/jquery.ajaxfileupload.js"></script>
//HTML部分
<div class="upld">
<div class="col-xs-3 box">
<a href="javascript:;" class="a-upload">
<input type="file" name="file[]" id="">
</a>
<p>+</p>
<!-- <a href="javascript:;" class="file">选择文件
<input type="file" name="" id="">
</a> -->
</div>
<div class="col-xs-3 box">
<a href="javascript:;" class="a-upload">
<input type="file" name="file[]" id="">
</a>
<p>+</p>
</div>
<div class="col-xs-3 box">
<a href="javascript:;" class="a-upload">
<input type="file" name="file[]" id="">
</a>
<p>+</p>
</div>
</div>
//jquery部分
$("input[type='file']").ajaxfileupload({
action: "{:U('Portal/News/ajaxFileUpload')}",
params: {
extra: 'info'
},
onComplete: function(res) {
if (res.code == '0'){
path = res.res['0'];
var img = "<img src='"+path+"'>";
$(this).parent().next().html(img)
} else{
alert(res.msg)
}
},
});
php部分:
public function ajaxFileUpload(){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 10485760 ;// 设置附件上传大小 10M
$upload->exts = array('zip', 'rar', '7z','jpg','jpeg','png','gif');// 设置附件上传类型
$upload->rootPath = C('UPLOADPATH'); // 设置附件上传根目录
$upload->savePath = 'guestbook/'; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
ajaxReturn('1',$info);
}else{// 上传成功
$re = array();
foreach ($info as $k => $v) {
$re[] = sp_get_asset_upload_path($v['savepath'] . $v['savename']);
}
ajaxReturn('0','success',$re);
}
}
private function upload(){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 10485760 ;// 设置附件上传大小 10M
$upload->exts = array('zip', 'rar', '7z','jpg','jpeg','png','gif');// 设置附件上传类型
$upload->rootPath = C('UPLOADPATH'); // 设置附件上传根目录
$upload->savePath = 'guestbook/'; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
return '';
}else{// 上传成功
$re = array();
foreach ($info as $k => $v) {
$re[] = $v['savepath'] . $v['savename'];
}
return $re;
}
}