1

前端上传文件或者上传文件夹

 1 year ago
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.

前端上传文件或者上传文件夹

精选 原创

彭世瑜 2022-12-19 14:27:38 ©著作权

文章标签 插入图片 HTML 上传文件 文章分类 Html/CSS 前端开发 yyds干货盘点 阅读数164

上传文件夹,主要的参数webkitdirectory

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

前端上传文件或者上传文件夹_HTML
兼容性
 https://caniuse.com/?search=webkitdirectory
前端上传文件或者上传文件夹_上传文件_02
<!-- 选择文件 -->
<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>
  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK