上传文件限制文件类型

   const beforeUpload = (file) => {
    return new Promise((resolve, reject) => {
      let acceptList = [".xls",".xlsx",".pdf",".doc",".docx",".jpg",".jpeg",".png",".rar",".zip"];
    if (file.name.indexOf('.') !== -1) {
      // 如果存在.
      let suffixList = file.name.split('.');
      let suffix = '.'+suffixList[suffixList.length - 1];
      console.log(suffix);
      if (!acceptList.includes(suffix)) {
        message.error("禁止上传此类型文件");
        return false;
      }
    } else {
      message.error("禁止上传无后缀文件");
      return false;
    }
      let res = true;
      if (fileShowList.length > 0) {
        fileShowList.map((item) => {
          if (item.name === file.name) {
            message.error(item.name + '此文件已经上传');
            res = false;
          }
        });
      }
      const isLt5M = file.size / 1024 / 1024 < 20;
      if (!isLt5M) {
        message.error('上传文件不能超过 20MB!');
      }
      if (res && isLt5M) {
        resolve();
      } else {
        reject('');
      }
    });
  };

 

posted @ 2022-06-22 16:53  天官赐福·  阅读(67)  评论(0编辑  收藏  举报
返回顶端