html上传图片并在前端显示

<input type="file" class="form-control" name="file" id="input_img">
<img id="show_img" src="" style="width: 150px;height: 100px">

<script>
  var getUserPhoto = document.getElementById("input_img");
  alert(getUserPhoto);
  getUserPhoto.onchange = function ()
  {
    var file = this.files;
    console.log(file);
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function ()
    {
      var image = document.getElementById("show_img");
      image.src = reader.result;
    };
  };
</script>

 

posted @ 2024-03-19 20:53  橘子味芬达水  阅读(86)  评论(0编辑  收藏  举报