很方便的后台ajax上传文件

时间:2021-07-02 07:31:53
<a href="javascript:void(0);" url="{:U('teacherd?id='.$vo['id'])}" class="teacherd" uid="{$vo.id}"><if condition="$vo.spstatus eq 0" >上传批阅<else /> 已批阅 </if></a>

            $(".teacherd").click(function () {
var $this=$(this);
var id = $(this).attr("uid");
var url = $(this).attr('url');
var p_id = "{:I('p_id')}";
var display = 'none';
var src = '';
var content = '<div class="margin_left_10" style="margin:20px;"><input type="hidden" name="id" value="' + id + '" ><div class="form-item"><div class="controls"><label class="label">上传文件:<span class="check-tips" id="picture-size"></span></label><input type="file" id="upfile"><input type="hidden" name="photo" id="cover_id_picture_id"/><div class="upload-img-box"><div class="upload-pre-item" style="display:' + display + ';"><img src="' + src + '"/></div></div></div></div></form></div>';
layer.open({
type: ,
title: '上传审批',
content: content,
closeBtn: ,
area: ['350px', '250px'],
btn: '确定',
yes: function () {
var fd = new FormData();
var uname = $('#upfile').val().split('.'); //获取上传文件名并拆封成数组(获取后缀)
if((uname[] != 'docx') && ( uname[] != 'doc')){ alert(uname[]);
return false;
}
fd.append("upload", );
fd.append("id", id); //将id传入
fd.append("p_id",p_id); //p_id
fd.append("upfile", $("#upfile").get().files[]);
$.ajax({
url: "{:U('uploadss')}",
type: "POST",
processData: false,
contentType: false,
data: fd,
success: function (d) {
if(d.code == ){
$(".layui-layer-shade").hide();
$(".layui-layer-page").hide();
$(".bac").fadeIn();
$(".bac1").html("上传成功")
$this.html("已审批")
}else{
$("#layui-layer-shade1").hide();
$("#layui-layer1").hide();
$(".bac").fadeIn();
$(".bac1").html("上传失败")
}
// console.log(d);
} })
}
})
}); $(".closeA").on("click",function(){
$(".bac").fadeOut()
})
$(".layui-layer-btn0").on("click",function(){
$(".layui-layer-shade").hide();
$(".layui-layer-page").hide();
})

后台代码:

 /*
* 上传审批
* Carey
*/ public function uploadss() {
$id = $_POST['id']; //id
$p_id = $_POST['p_id']; $exam_listsDB = M('exam_lists');
$username = $exam_listsDB->where(array('id' => $id))->getField('username'); $username = iconv("UTF-8", "gb2312", $username); //转码utf8
$saveName = $username . '_' . $p_id . '_' . $id; //文件命名规则考场p_id_用户id; $rootpath = '../Public/Uploads/examsp/kc' . $p_id . '/';
mkdir($rootpath, ); $upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = ; // 设置附件上传大小 3M
$upload->exts = array('application/msword', 'doc', 'docx'); // 设置附件上传类型
$upload->rootPath = $rootpath; // 设置附件上传根目录 // 上传文件
$upload->autoSub = TRUE; //自动生成子目录
$upload->subName = 'py';
$upload->saveName = $saveName; //根据p_id-id
$upload->replace = TRUE; //存在同名是否覆盖 true覆盖 $info = $upload->uploadOne($_FILES['upfile']); if (!$info) {// 上传错误提示错误信息
$redata = array(
'code' => , 'info' => $info, 'files' => $_FILES, 'id' => $id, 'p_id' => $p_id, 'username' => $username,
);
} else {
// 上传成功
$info['savename'] = iconv("gb2312", "UTF-8", $info['savename']); //转码utf8
$cha_dourl['cha_dourl'] = 'Public/Uploads/examsp/' . $info['savepath'] . $info['savename'];
$cha_dourl['sptime'] = time();
$cha_dourl['spstatus'] = ;
/* 如果上传成功更新表单数据 */
$upda = $exam_listsDB->where(array('id' => $id))->save($cha_dourl);
if ($upda) {
$redata = array(
'code' => , 'info' => $info, 'files' => $_FILES, 'id' => $id, 'p_id' => $p_id, 'upda' => $upda,
);
} else {
$redata = array(
'code' => , 'info' => $info, 'files' => $_FILES, 'id' => $id, 'p_id' => $p_id, 'upda' => $upda,
);
}
}
$this->ajaxReturn($redata);
}