HTML代码:
需要一个input:file图片上传控件
<input name="filez" onchange="picture(this);" type="file" id="License" />
<div class="goodsimgz">
<img src="img/addimg.svg" alt="">
</div>
js代码:
// 上传图片
let imgUrl = '';
function picture() {
var formData = new FormData();
formData.append("file", $("#License")[0].files[0]);
$.ajax({
url: "接口",
type: "post",
data: formData,
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false, // 告诉jQuery不要去设置Content-Type请求头
Cache: false,
// dataType: 'text',
success: function(res) {
console.log(res);
imgUrl = res.url;
$(".goodsimgz img").attr("src", imgUrl);
},
error: function(res) {
}
})
}