html:
<div class="cert_img"> <label> <input type="file" name="" onchange="fileImg()" id="certFrom"> <img src="./img/选择图片.png" alt="" id="certImg"> </label> </div>
css:
.cert_img{ width: 80%; height: 170px; margin-left: 10px; } .cert_img input{ display: none }
js:
<script> function fileImg() { var formData = new FormData(); formData.append("file", $("#certFrom")[0].files[0]); $.ajax({ url: "接口地址", type: 'POST', data: formData, dataType: 'json', cache: false, processData: false, //不要处理发送的数据 contentType: false, //不要添加请求头 success: function(data) { console.log(data); $('#certImg').attr("src",`http://域名+{data.url}`) //上传图片预览 }, error: function(data) { console.log(data); } }); } </script>