ajax<input>标签文件异步上传
ajax代码
function uploadImg() {
console.log($.fn);
let formData = new FormData();
formData.append("img",document.getElementById("goods_img").files[0]);
$.ajax({
url: "goods/upload",
type:"post",
data:formData,
contentType:false,
processData:false,
dataType: "json",
success: function (data) {
console.log($.fn);
$("#img_div").html(" ");
let imgObg = $("<img>");
imgObg.attr("src", "../../upload/goods/img/" + data.imgName);
imgObg.attr("width", "60px");
imgObg.attr("height", "60px");
$("#img_div").append(imgObg);
},
error: function (data) {
alert(data.messages);
}
})
}
界面代码
<input onchange="uploadImg()" id="goods_img" name="img" type="file" class="form-control-file" id="exampleFormControlFile">