Vue implements sample code for dragging files from desktop to web page (can display pictures/audio/video)

Vue implements sample code for dragging files from desktop to web page (can display pictures/audio/video)

Effect

insert image description here

insert image description here

If you use it, please optimize the code and style yourself

The code for not displaying pictures/playing video and audio is as follows

<template>
 <div>
  <div v-on:dragover="tts" v-on:drop="ttrs" style="width: 800px;height: 200px;border: 1px solid black;font-size: 40px;line-height: 200px">
   {{dt}}
  </div>
  <div v-for="(item,index) in fileList" :key="index" style="width: 800px;height: 200px;border: 1px solid black;font-size: 40px;position: relative;top:10px">
   <p style="font-size: 20px;float: left;position: relative;left: 20pxword-wrap:break-word;word-break:normal;">{{item.name}}</p>
   <h5 style="float:right;position: absolute;top: 80px;right: 20px">{{item.type}}</h5>
   <h6 style="position: absolute;top: 80px;float: left;left: 20px">{{item.size | sizeType}}</h6>
   <button style="float: right" @click="del(index)">Delete</button>
  </div>
 </div>
</template>

<script>
 export default {
  name: "trs",
  data(){
   return {
    dt:"",
    fileList:[]
   }
  },
  filters:
   sizeType(val){
    let kbs = val/1024;
    let mbs = 0;
    let gbs = 0;
    if(kbs>=1024){
     mbs = kbs/1024;
    }
    if(mbs>=1024){
     GBS = MBS/1024
     return gbs.toFixed(2)+"GB";
    }else if (mbs>=1){
     return mbs.toFixed(2)+"MB"
    }else {
     return kbs.toFixed(2)+"KB"
    }
   }
  },
  mounted() {
   let vm = this;
   window.addEventListener("dragdrop", this.testfunc, false);


   document.addEventListener("dragover",function () {
    console.log(111)
    vm.dt = "Drag here to upload files"
    console.log(vm.dt)
   })
  },
  methods:{
   testfunc(event) {
    alert("dragdrop!");
    event.stopPropagation();
    event.preventDefault();
   },
   del(index){
    this.fileList.splice(index,1)
    if(this.fileList.length==0){
     this.dt = ""
    }
   },
   tts(e){
    console.log(e)
    this.dt = "Drag here to upload the file"
   },
   ttrs(e){
    console.log(e)
    console.log(e.dataTransfer.files)
    let datas = e.dataTransfer.files;
    datas.forEach(item=>{
     this.fileList.push(item)
    })
    e.stopPropagation();
    e.preventDefault();
    this.dt = "Upload completed, you can continue uploading"

   }
  }
 }
</script>

<style scoped>

</style>

If you want to display pictures/play videos/play audio

Here I display/play the last uploaded file by default, you can modify it according to your needs

<template>
 <div>
 <div
  v-on:dragover="tts"
  v-on:drop="ttrs"
  style="width: 800px;height: 200px;border: 1px solid black;font-size: 40px;line-height: 200px"
 >
  {{ dt }}
 </div>
 <div
  v-for="(item, index) in fileList"
  :key="index"
  style="width: 800px;height: 200px;border: 1px solid black;font-size: 40px;position: relative;top:10px"
 >
  <p
  style="font-size: 20px;float: left;position: relative;left: 20pxword-wrap:break-word;word-break:normal;"
  >
  {{ item.name }}
  </p>
  <h5 style="float:right;position: absolute;top: 80px;right: 20px">
  {{ item.type }}
  </h5>
  <h6 style="position: absolute;top: 80px;float: left;left: 20px">
  {{ item.size | sizeType }}
  </h6>
  <button style="float: right" @click="del(index)">Delete</button>
 </div>
 <div style="position:relative;top: 100px">
  <img v-if="isImage" :src="srcs" style="width: 800px" />
  <video v-if="isVideo" controls :src="srcs" style="width: 800px"></video>
  <audio v-if="isAudio" controls :src="srcs" style="width: 800px"></audio>
 </div>
 </div>
</template>

<script>
export default {
 name: "trs",
 data() {
 return {
  dt: "",
  fileList: [],
  srcs:"",
  isImage:false,
  isAudio:false,
  isVideo:false
 };
 },
 filters:
 sizeType(val) {
  let kbs = val / 1024;
  let mbs = 0;
  let gbs = 0;
  if (kbs >= 1024) {
  mbs = kbs / 1024;
  }
  if (mbs >= 1024) {
  gbs = mbs / 1024;
  return gbs.toFixed(2) + "GB";
  } else if (mbs >= 1) {
  return mbs.toFixed(2) + "MB";
  } else {
  return kbs.toFixed(2) + "KB";
  }
 }
 },
 mounted() {
 let vm = this;
 window.addEventListener("dragdrop", this.testfunc, false);

 document.addEventListener("dragover", function() {
  console.log(111);
  vm.dt = "Drag here to upload files";
  console.log(vm.dt);
 });
 },
 methods: {
  readFile(file){
   let vm = this;
   let reader = new FileReader();
   reader.readAsDataURL(file)
   reader.onload = function () {
    let type = file.type.substr(0,5);
    if(type=="image"){
     vm.isImage = true;
     vm.isAudio =false;
     vm.isVideo = false;
    }else if(type=="audio"){
     vm.isImage = false;
     vm.isAudio = true;
     vm.isVideo = false;
    }else if(type=="video"){
     vm.isImage = false;
     vm.isAudio = false;
     vm.isVideo = true;
    }else {
     alert("Not a picture/video/audio")
    }
    vm.srcs = reader.result;
    // this.$nextTick(()=>{
    //
    // })
   }
  },
 testfunc(event) {
  alert("dragdrop!");
  event.stopPropagation();
  event.preventDefault();
 },
 del(index) {
  this.fileList.splice(index, 1);
  if (this.fileList.length === 0) {
  this.dt = "";
  }
 },
 tts(e) {
  console.log(e);
  this.dt = "Drag here to upload the file";
 },
 ttrs(e) {
  console.log(e);
  console.log(e.dataTransfer.files);
  let datas = e.dataTransfer.files;
  datas.forEach(item => {
  this.fileList.push(item);
  });
  this.readFile(this.fileList[this.fileList.length-1])

  e.stopPropagation();
  e.preventDefault();

  this.dt = "Upload completed, you can continue uploading";
 }
 }
};
</script>

<style scoped></style>

This is the end of this article about how to use Vue to drag files from the desktop to the web page (can display pictures/audio/video). For more related Vue dragging file content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • electron+vue realizes div contenteditable screenshot function
  • Vue opens a new window and implements a graphic example of parameter transfer
  • Steps to encapsulate the carousel component in vue3.0
  • Vue basic instructions example graphic explanation
  • Vue imitates Ctrip's carousel effect (sliding carousel, highly adaptive below)
  • vue+echarts realizes the flow effect of China map (detailed steps)
  • An example of refactoring a jigsaw puzzle game using vue3
  • How to use bar charts in Vue and modify the configuration yourself
  • Vue implements irregular screenshots

<<:  How to export mysql query results to csv

>>:  Alibaba Cloud Server Ubuntu Configuration Tutorial

Recommend

Web lesson plans, lesson plans for beginners

Teaching Topics Web page Applicable grade Second ...

WeChat Mini Program QR Code Generation Tool weapp-qrcode Detailed Explanation

WeChat Mini Program - QR Code Generator Download:...

Implementing parameter jump function in Vue project

Page Description:​ Main page: name —> shisheng...

Beginners learn some HTML tags (2)

Related article: Beginners learn some HTML tags (1...

Detailed tutorial on installing Ubuntu 19.10 on Raspberry Pi 4

Because some dependencies of opencv could not be ...

Pygame code to make a snake game

Table of contents Pygame functions used Creating ...

How to implement digital paging effect code and steps in CSS

A considerable number of websites use digital pagi...

CSS eight eye-catching HOVER effect sample code

1. Send effect HTML <div id="send-btn&quo...

Linux file and user management practice

1. Display the files or directories in the /etc d...

Solution to Element-ui upload file upload restriction

question Adding the type of uploaded file in acce...

JavaScript implements the detailed process of stack structure

Table of contents 1. Understanding the stack stru...

How to use HTML form with multiple examples

Nine simple examples analyze the use of HTML form...

How to query and update the same table in MySQL database at the same time

In ordinary projects, I often encounter this prob...

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Table of contents 1. Preparation 2. Define the gl...