![](/style/images/good.png)
1
![](/style/images/bad.png)
前端上传文件或者上传文件夹
source link: https://blog.51cto.com/mouday/5952642
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
前端上传文件或者上传文件夹
精选 原创上传文件夹,主要的参数webkitdirectory
浏览器上传文件夹,浏览器会弹出询问窗口
![前端上传文件或者上传文件夹_HTML](https://s2.51cto.com/images/blog/202212/19142730_63a00452f1d6796920.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/format,webp/resize,m_fixed,w_1184)
https://caniuse.com/?search=webkitdirectory
![前端上传文件或者上传文件夹_上传文件_02](https://s2.51cto.com/images/blog/202212/19142731_63a00453268d043242.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/format,webp/resize,m_fixed,w_1184)
<!-- 选择文件 -->
<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>
- 打赏
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK