thinkphp5 不使用form,用input+ajax异步上传图片
不支持$this->request->file()获取图片
后台接收文件请使用$_FILE
正文开始:
HTML
<div class="upload">
<div class="box-upload">
<span>上传图片</span>
</div>
<input type="file" accept="image/*" name="img" class="uploadImg" value="">
<img src="" alt="" id="imgimg">
</div>
JQUERY
$(".uploadImg").on("change",function () {
var formData = new FormData();
formData.append('img',this.files[0])
$.ajax({
url:'/module/controller/uploadImg',
data:formData,
contentType: false,
processData: false,
type:'post',
dataType:'json',
success:function (res) {
console.log(res)
}
})
})