惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
WordPress大学
WordPress大学
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
D
Docker
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
U
Unit 42
M
MIT News - Artificial intelligence
B
Blog
GbyAI
GbyAI
C
Check Point Blog
P
Proofpoint News Feed
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
IT之家
IT之家
Google DeepMind News
Google DeepMind News
V
V2EX
Stack Overflow Blog
Stack Overflow Blog

博客园 - Xproer-松鼠

PHP实现视频文件上传完整实例 ueditor 富文本编辑器粘贴图片时让图片居中 TinyMCE富文本编辑器粘贴图片自动上传问题解决 UEditor富文本编辑器图片粘贴和上传问题 vue项目中使用tinymce富文本编辑器实现图片上传/粘贴格式 富文本编辑器复制word文档中的图片 富文本编辑器:自己实现图片上传功能和图片粘贴上传(kindeditor) 前端实现文件上传(点击+拖拽) HTML5应用之文件拖拽上传 使用HTML5实现多文件上传 HTML5 文件上传的2种方式 html5实现文件批量上传组件 HTML5文件上传操作 html5中怎么实现多文件上传功能 HTML5 进阶系列:文件上传下载 html实现上传 大文件分片上传 【前后台完整版】大文件分片上传 大文件、视频分片上传,断点续传
前端上传文件或者上传文件夹
Xproer-松鼠 · 2023-12-26 · via 博客园 - Xproer-松鼠

文档

https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input
上传文件夹,主要的参数webkitdirectory

浏览器上传文件夹,浏览器会弹出询问窗口

兼容性
https://caniuse.com/?search=webkitdirectory


代码如下

<!-- 选择文件 -->
<div>
<label for="upload-file">点击选择文件</label>
<input
title="点击选择文件"
id="upload-file"
multiple=""
accept="*/*"
type="file"
name="html5uploader"
/>
</div>

<!-- 选择文件夹 -->
<div style="margin-top: 20px">
<label for="upload-directory">点击选择文件夹</label>
<input
title="点击选择文件夹"
id="upload-directory"
multiple=""
webkitdirectory=""
accept="*/*"
type="file"
name="html5uploader"
/>
</div>

<script>
// 选择文件
document
.querySelector('#upload-file')
.addEventListener('input', function (event) {
for (let file of event.target.files) {
console.log(file)
}
})

// 选择文件夹
document
.querySelector('#upload-directory')
.addEventListener('input', function (event) {
for (let file of event.target.files) {
console.log(file)
// 属性 webkitRelativePath 有值
}
})
</script>

参考文章:http://blog.ncmem.com/wordpress/2023/12/26/%e5%89%8d%e7%ab%af%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%e6%88%96%e8%80%85%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%e5%a4%b9/

欢迎入群一起讨论