html 使用input file上传图片并显示

<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

    <title>文件上传</title>

  </head>

  <body>

    <div id="app">

        <input type="file" name="" id="input_img" />

        <div id="show_img" style="width: 400px;height: 400px;display: flex;align-items: center;justify-content: center;overflow: hidden;"></div>

       

    </div>

   <script>

    var getUserPhoto = document.getElementById("input_img");

      getUserPhoto.onchange = function () {

        var file = this.files;

        console.log(file);

        var reader = new FileReader();

        reader.readAsDataURL(file[0]);

        reader.onload = function () {

          var image = document.createElement("img");

          image.width = "400";

          image.src = reader.result;

          var showPicture = document.getElementById("show_img");

          showPicture.append(image);

        };

      };

   </script>

  </body>

</html>

相关推荐

  1. html 使用input file图片显示

    2024-06-12 20:16:01       11 阅读
  2. html字符串中的base64图片转换成file

    2024-06-12 20:16:01       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 20:16:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 20:16:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 20:16:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 20:16:01       20 阅读

热门阅读

  1. 软件许可证管理?

    2024-06-12 20:16:01       8 阅读
  2. 如何有效限制IP多次重新访问网站

    2024-06-12 20:16:01       9 阅读
  3. MySQL 运算符绕过

    2024-06-12 20:16:01       4 阅读
  4. 【react】useState 使用指南

    2024-06-12 20:16:01       8 阅读
  5. linux常用的基础命令

    2024-06-12 20:16:01       6 阅读
  6. CountDownLatch闭锁

    2024-06-12 20:16:01       6 阅读
  7. uniapp怎么实现条形码

    2024-06-12 20:16:01       7 阅读