<input type="file" name="file" id="file" class="form-control">
<img src="" style="width: 50%" id="file-img">
<script>
document.getElementById('file').addEventListener('change', function (event) {
var $file = event.currentTarget;
var formData = new FormData();
var file = $file.files;
formData = new FormData();
formData.append('img', file[0]);
$.ajax({
url: '',
type: 'POST',
dataType: 'json',
data: formData,
contentType: false,
processData: false,
success:function(data){
if(data.status == 'success')
{
var path = data.url;
$("#file-img").attr("src", path) ; //将图片路径存入src中,显示出图片
}
},
error:function(data)
{
console.log(data)
}
})
});
</script>
js提交图片
最新推荐文章于 2021-12-06 21:54:52 发布