Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

thinkphp3.2.3 ajax上传图片

控制器代码

clipboard.png

html代码

clipboard.png

怎么改能按了提交就显示出来?这个thinkphp这个post数据是怎么用的,怎么我在控制器完全不用post过来数据呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ajax上传文件需要使用formData对象

var formData = new FormData($( "form" )[0]);  
$.ajax({  
       url: '__CONTROLLER__/upload' ,  
       type: 'POST',  
       data: formData,  
       dataType:'json',
       async: false,  
       cache: false,  
       contentType: false,  
       processData: false,  
       success: function (response) {  
           if(response.status == 1){
               //判断返回的status来确定文件是否上传成功,以及上传成功后要做的操作
           }
           alert(response.msg);  
       },  
});  

php代码最后得改一下

if(!$info){
    $data = [
        'status'=>0,
        'msg'=>'上传失败,'.$upload->getError()
    ];
}else{
    $data = [
        'status'=>1,
        'msg'=>'上传成功'
    ];
}
echo json_encode($data);die;   //这样ajax用json接收才能正确弹出php返回的信息
//或者return $this->ajaxReturn($data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...