0
点赞
收藏
分享

微信扫一扫

html+JS+Jquery实现视频/图片上传并预览

我阿霆哥 2022-05-01 阅读 69

可用于asp.net

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <title>Document</title>
  </head>
  <body>
    <div>
      <input type="file" id="file0" />
      <img id="img0" width="200" height="200" />
      <video id="video0" width="200" height="200" controls="controls" autoplay="autoplay"></video>
    </div>
    <script>
      //监听input的change事件
      $("#file0").change(function () {
        var objUrl = getObjectURL(this.files[0]);
        console.log("objUrl = " + objUrl);
        if (objUrl) {
          $("#img0").attr("src", objUrl);
          $("#video0").attr("src", objUrl);
        }
      });

      //建立一個可存取到該file的url
      function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL != undefined) {
          // basic
          url = window.createObjectURL(file);
        } else if (window.URL != undefined) {
          // mozilla(firefox)
          url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) {
          // webkit or chrome
          url = window.webkitURL.createObjectURL(file);
        }
        return url;
      }
    </script>
  </body>
</html>
举报

相关推荐

0 条评论