当前位置: 首页 > news >正文

前端手写文件上传;使用input实现文件拖动上传

使用input实现文件拖动上传

在这里插入图片描述

vue2代码:

<template><div><div class="drop-area" @dragenter="highlight" @dragover="highlight" @dragleave="unhighlight" @drop="handleDrop"@click="handleClick">将文件拖拽到此处或者点击上传<input type="file" ref="fileInput"  accept=".png,.jpg,.jpeg,.pdf" @change="handleFiles" multiple style="display: none;"></div><ul v-if="fileList.length"><li v-for="(file, index) in fileList" :key="index">{{ file.name }}</li></ul><pre>{{ fileList }}</pre></div>
</template><script>
export default {data() {return {fileList: []};},methods: {highlight(e) {e.preventDefault();e.stopPropagation();e.target.classList.add('hover');},unhighlight(e) {e.target.classList.remove('hover');},handleDrop(e) {e.preventDefault();e.stopPropagation();this.unhighlight(e);const files = e.dataTransfer.files;console.log('拖拽得到List', e);// this.handleFiles(files);for (let i = 0; i < files.length; i++) {this.fileList.push({...files[i],name: files[i].name,size: files[i].size,type: files[i].type});}this.uploadFile()},handleClick() {this.$refs.fileInput.click();},handleFiles(e) {console.log(11, e);const files = e.target.files || e;for (let i = 0; i < files.length; i++) {this.fileList.push({...files[i],name: files[i].name,size: files[i].size,type: files[i].type});}this.uploadFile()},// 上传文件uploadFile() {console.log('this.fileList', this.fileList);}}
};
</script><style>
.drop-area {width: 300px;height: 200px;border: 2px dashed #ccc;border-radius: 10px;text-align: center;line-height: 200px;cursor: pointer;
}.drop-area.hover {border-color: #11716f;
}.drop-area:hover {border-color: #11716f;
}
</style>

vue3代码:

<template><div><div class="drop-area" @dragenter="highlight" @dragover="highlight" @dragleave="unhighlight" @drop="handleDrop"@click="handleClick">将文件拖拽到此处或者点击上传<input type="file" ref="fileInput" accept=".png,.jpg,.jpeg,.pdf" @change="handleFiles" multiple style="display: none;"></div><ul v-if="fileList.length"><li v-for="(file, index) in fileList" :key="index">{{ file.name }}</li></ul><pre>{{ fileList }}</pre></div>
</template><script>
import { ref } from 'vue';export default {setup() {const fileList = ref([]);const fileInput = ref(null);const highlight = (e) => {e.preventDefault();e.stopPropagation();e.target.classList.add('hover');};const unhighlight = (e) => {e.target.classList.remove('hover');};const handleDrop = (e) => {e.preventDefault();e.stopPropagation();unhighlight(e);const files = e.dataTransfer.files;for (let i = 0; i < files.length; i++) {fileList.value.push({...files[i],name: files[i].name,size: files[i].size,type: files[i].type});}uploadFile();};const handleClick = () => {console.log(fileInput.value);fileInput.value.click(); // 使用 ref 的 value 直接访问 DOM 元素};const handleFiles = (e) => {const files = e.target.files || e;for (let i = 0; i < files.length; i++) {fileList.value.push({...files[i],name: files[i].name,size: files[i].size,type: files[i].type});}uploadFile();};// 上传文件const uploadFile = () => {console.log('fileList', fileList.value);};return {fileList,highlight,unhighlight,handleDrop,handleClick,handleFiles,fileInput};}
};
</script><style>
.drop-area {width: 440px;height: 185px;border: 1px dashed #dcdfe6;border-radius: 6px;text-align: center;line-height: 185px;cursor: pointer;
}.drop-area.hover {border-color: #11716f;
}.drop-area:hover {border-color: #11716f;
}
</style>
http://www.lryc.cn/news/357532.html

相关文章:

  • Flutter 中的 PhysicalModel 小部件:全面指南
  • Flutter 中的 Center 小部件:全面指南
  • windows 执行node报错 800A1391
  • 无人机操作界面来了,起点就很高呀。
  • Android 11 AudioPolicyService 启动流程
  • java中static关键字面试五连问
  • 基于文本来推荐相似酒店
  • 红队内网攻防渗透:内网渗透之前置知识外网权限提升技术
  • 【漏洞复现】大华智能物联综合管理平台 log4j远程代码执行漏洞
  • OrangePi AIpro测评
  • 写代码之前一定要提前想好思路
  • 「清新题精讲」Skiers
  • Transformer详解(8)-基于transformer的英文到中文翻译模型
  • 算法的时间复杂度(详解)
  • Flutter 中的 NestedScrollViewViewport 小部件:全面指南
  • 断开自定义模块与自定义库的链接
  • 粉丝问,有没有UI的统计页面,安排!
  • Nginx R31 doc-17-debugging 调试
  • python -【一】基础语法
  • 数据结构 | 详解二叉树——堆与堆排序
  • vb.net,C#强制结束进程,“优雅”的退出方式
  • 牛客热题:数据流中的中位数
  • JavaScript-JavaWeb
  • vue组件通讯$parent和$children获取单签组件的⽗组件和当前组件的⼦组件的例子
  • Util和utils
  • 拷贝构造、移动构造、拷贝赋值、移动赋值
  • Python3 笔记:math模块
  • python -【四】函数
  • 力扣 5. 最长回文子串 python AC
  • 【微机原理及接口技术】可编程计数器/定时器8253