文件上传面板中限制需要的文件格式,并且隐藏“所有文件”选项

直接说需求:需要实现在文件上传面板中限制需要的文件格式,并且不想展示“所有文件”这个选项,应该怎么做嘞?效果如下图:

这里用到了 window.showOpenFilePicker 方法实现,首先定义接受的格式及限制:

const pickerOpts = {
  types: [
    {
      description: 'Files',
      accept: 
        {
          'application/pdf': ['.pdf'],
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx', '.doc'],
          'application/vnd.ms-excel': ['.xls'],
          'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
          'application/vnd.openxmlformats-officedocument.presentationml.presentation': ['.pptx','.ppt']
        }
    }
  ],
  excludeAcceptAllOption: true,
  multiple: state.allowMultipleUpload
};

这里如果按照下面这种写法来写,会导致出现.ppa和.dot格式的文件格式:

const pickerOpts = {
  types: [
    {
      description: 'Files',
      accept: 
        {
          'application/pdf': ['.pdf'],
          'application/vnd.ms-word': ['.doc'],
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx'],
          'application/vnd.ms-excel': ['.xls'],
          'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
          'application/vnd.ms-powerpoint': ['.ppt'],
          'application/vnd.openxmlformats-officedocument.presentationml.presentation': ['.pptx']
        }
    }
  ],
  excludeAcceptAllOption: true,
  multiple: state.allowMultipleUpload
};

下面是实现效果的关键代码:

try {
    if (window.showOpenFilePicker) {
      const fileHandles = await window.showOpenFilePicker(pickerOpts);
      const fileDatas = await Promise.all(fileHandles.map(fileHandle => fileHandle.getFile()));
      handleFileInput(fileDatas); // 这里进行后续的上传逻辑
    } else {
      const input = document.createElement('input');
      input.type = 'file';
      input.multiple = true;
      input.accept = '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx';
      input.onchange = () => {
        if (input.files) {
          handleFileInput(Array.from(input.files)); // 这里进行后续的上传逻辑
        }
      };
      input.click();
    }
} catch (err) {
    console.log(err);
}

这里为什么是加了if..else,因为Safari浏览器和火狐浏览器不支持 window.showOpenFilePicker,因此需要向下兼容,使用原生的方法,不过这两个浏览器无法去掉“所有文件”这个选项,效果如下:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

低保和光头哪个先来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值