Home > front end >  I'm getting an invalid file name format error upon uploading of the file
I'm getting an invalid file name format error upon uploading of the file

Time:02-10

I am working on ant-design form. I want to validate the file name format of the excel file that I've uploaded but always getting an "Invalid file name format" error in return, can you please help to check my code for the filename validation that I've done:

checkFileName = (_filename) => {
    const filename = _filename.split('.').slice(0, -1).join('.');
    const regexString = '._bulkmodification_signcontract_mpqrs_.{8}';
    const result = this.bulkModificationProcessFormat(filename, regexString);
    if (result) {
      return true;
    } else {
      return false;
    }
  };

  bulkModificationProcessFormat = (_filename, _regex) => {
    console.log('bulkModificationProcessFormat: '   _filename);
    const formatRegex = new RegExp(_regex);
    const isValidRegex = formatRegex.test(_filename);
    if (isValidRegex) {
      const dateValue = _filename.split('_').slice(2);
      const dateArray = dateValue[0].split('');
      const mm = dateArray[0]   dateArray[1];
      const dd = dateArray[2]   dateArray[3];
      const yyyy = dateArray[4]   dateArray[5]   dateArray[6]   dateArray[7];
      const mmddyyyy = mm   '/'   dd   '/'   yyyy;
      const dateObject = new Date(mmddyyyy);
      console.log('mmddyyyy: '   mmddyyyy);
      if (isNaN(dateObject.getTime())) {
        return false;
      } else {
        return true;
      }
    } else {
      return false;
    }
  }

return (
      <Dragger
        {...fileUploadprops}
        onChange={(info) => this.handleFile(info)}
        beforeUpload={(file) => {
          this.setState({ draggerKey: 1 });
          this.setState({ filename: file.name });
          const isValidFileName = this.checkFileName(file.name);
          if (!isValidFileName) {
            message.warning(CommonConstant.BULK_MERCHANT_INVALID_FILENAME);
            this.setState({ draggerKey: 0 });
            return false;
          }
        </Dragger>
    );
  };

Example filename - 001_bulkmodification_signcontract_mpqrs_10312021.xls

CodePudding user response:

  •  Tags:  
  • Related