拖拽上传(预览图片)

需求

点击+上传图片,或直接拖拽图片到红色方框里面也可上传图片,上传后预览图片

效果

在这里插入图片描述

实现

<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片文件拖拽上传</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    img {
      display: block;
      width: 200px;
      height: auto;
    }

    .show-img {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 800px;
      height: 320px;
      margin: 100px auto;
      border: 1px solid red;
    }

    .container {
      display: flex;
    }

    .upload {
      position: relative;
      width: 100px;
      height: 100px;
      border: 2px dashed #ccc;

    }

    .upload::before,
    .upload::after {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      margin: auto;
      background-color: black;
    }

    .upload::before {
      width: 20px;
      height: 4px;

    }

    .upload::after {
      width: 4px;
      height: 20px;
    }

    /* 触发file类型的Input上传文件不一定要点击上传按钮,点在Input身上即可 */
    #file-input {
      opacity: 0;
      box-sizing: border-box;
      width: 100px;
      height: 100px;
    }

    .preview {
      position: relative;
      width: 100px;
      height: 100px;
      margin: 0 20px;
      border-radius: 5px;
    }

    .preview-img {
      width: 100%;
      height: 100%;
    }

    .delete {
      position: absolute;
      width: 10px;
      height: 10px;
      top: -5px;
      right: -5px;
      color: red;
    }
  </style>
</head>

<body>
  <form class="container" action="">
    <div class="upload">
      <input type="file" id="file-input" hidden>
    </div>
    <div class="show-box"></div>
  </form>
  

  <div class="show-img"></div>
  <script>
    let input = document.querySelector('input[type="file"]');
    let showImg = document.querySelector('.show-img');
    let imgInput = document.querySelector('#file-input');
    let uploadBox = document.querySelector('.upload');

    //点击别的位置调用inputFile功能
    uploadBox.addEventListener('click',function(e){
      imgInput.click();
    },false);

    //拖拽上传
    showImg.addEventListener('dragover',function(e){
     e.preventDefault();
    },false);

    showImg.addEventListener('drop',function(e){
      e.preventDefault();
      let file =  e.dataTransfer.files[0];
      createBolbFile(file)
    },false);

  
    //点击input上传
    input.addEventListener('change', function (e) {
      console.log(input.files[0]);//拿到input当前上传的文件,拿不到url,想在页面展示必须有url
      let file = this.files[0];
      // createFileReader(file);
       createBolbFile(file)
    }, false);


    function createImg(src){
      let img = document.createElement('img');
      img.src = src;
      showImg.append(img);
    }

    //生成临时blob存储
    function createBolbFile(file){
      let url = URL.createObjectURL(file); //生成一个临时地址blob,不可持续
      //创建一个url的blob ,在当前会话下生效,不像base64那样在哪里都能用,如在浏览器中用,当你关掉浏览器再开浏览器用则不生效,是一种临时文件
      createImg(url)
    }

    //base64存储
    function createFileReader(file) {
      let fileReader = new FileReader();//通过FileReader的result可以拿到文件的url
      
      fileReader.readAsDataURL(file);//把文件创建成url的data对象

      fileReader.onload = function () { //file加载完成才可以拿到url
        let src = this.result  //base64存储
        createImg(src);
      }

    }


    /* 
      base64存储

      base64是新的文件协议格式,传输8bit字节码的编码方式,借助64个可以表示字符[A,B,C...1,2,3] 转换二进制 

      base64 字符串通用性很强 不依赖平台 不依赖环境 可持续 ,可以以字符串的形式传输图片、文档等
        
      可以很方便的用来展示图片,下载文档
      
      -----------------------------------------------

      blob存储

      如果要持续存储用base64,只是临时调用用blob
      blob网址URL

      只能在浏览器内部生成 

      URL/Object 允许Blob 和 file对象用作图像上 二进制数据连接URL源 

      URL.createObjectURL(file) 生成地址

      同一个会话中 同一个浏览器实例中可以使用 临时的对象地址 不可持续
    
    */
  </script>
</body>

</html>

相关推荐

  1. vue基于element封装图片

    2024-07-22 07:14:01       28 阅读
  2. js使用canvas实现图片鼠标滚轮放大缩小

    2024-07-22 07:14:01       42 阅读
  3. Vue【七】实现图片

    2024-07-22 07:14:01       34 阅读
  4. vue+element ui实现图片进行图片排序

    2024-07-22 07:14:01       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 07:14:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 07:14:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 07:14:01       45 阅读
  4. Python语言-面向对象

    2024-07-22 07:14:01       55 阅读

热门阅读

  1. 前端部署后提示用户刷新页面

    2024-07-22 07:14:01       16 阅读
  2. 编写测试用例:策略、技巧与最佳实践

    2024-07-22 07:14:01       17 阅读
  3. 自动化测试的艺术:Xcode中GUI测试的全面指南

    2024-07-22 07:14:01       17 阅读
  4. C++基础语法:STL之容器(6)--序列容器中的forward_list

    2024-07-22 07:14:01       15 阅读
  5. MongoDB Map-Reduce 简介

    2024-07-22 07:14:01       15 阅读
  6. 【SpringBoot】第3章 SpringBoot的系统配置

    2024-07-22 07:14:01       15 阅读
  7. Python中with 关键字、tell() 和 seek() 方法

    2024-07-22 07:14:01       16 阅读
  8. 初识数据结构中的“栈”

    2024-07-22 07:14:01       16 阅读
  9. 44、PHP 实现数据流中的中位数(含源码)

    2024-07-22 07:14:01       16 阅读
  10. Python面试题:Python中的单例模式及其实现

    2024-07-22 07:14:01       18 阅读
  11. JVM 中的OopMap与安全点

    2024-07-22 07:14:01       17 阅读
  12. 在 Ubuntu 22.04/20.04 安装 CVAT 和 SAM 指南

    2024-07-22 07:14:01       19 阅读