ajax上传图片
用jquery接收file数据然后有ajax提交到后台
1.首先HTML代码
<div class="form-group"> <label class="col-sm-2 control-label">图片</label> <div class="col-sm-4">
<input type="hidden" name="image" id="img_url" value="<?php echo $rows['image']; ?>"> <div id="file"> <span>更新图片</span>
<input type="file" onchange="showImg(this)" id="uploadForm"> </div> </div> <div class="col-sm-2"> <div class="box"> <section id="t_img">
<img src="<?php echo $rows['image']; ?>">
</section> </div> </div> </div>
2jQuery代码
function doUpload(){ if(img_l == ''){ }else{ var formData = new FormData($( "#formxx" )[0]); formData.append('image', img_l); formData.append('id', $('#p_id').val()); $.ajax({ url: 'index.php?m=admin&c=consult&f=ajax_up_img' , type: 'post', data: formData, dataType: "json", async: false, cache: false, contentType: false, processData: false, success:function(data,status){ $('#img_url').attr('value',data['img_url']); //alert(data['img_url']); }, error:function (returndata){ alert(returndata); } }); } return true; }
提交时执行函数即可,后台用$_flies['img'];获取或者是tp5的方法都行