a543462378 2022-04-20 10:12 采纳率: 84.7%
浏览 41
已结题

react导入excal文件,要怎么做前端验证?

react项目,做了前端导入功能,要求前端把数据转化一下然后验证再给后端,转化完成之后验证要怎么写?能不能写个例子或者推荐个例子?
文件读取如下

export default ({ setList, disabled }) => {

    function beforeUpload(file) {
        if (typeof file.name === 'string' && file.name.indexOf('xlsx') > -1) {
            read(file);
        } else {
            message.error('请上传xlsx文件,建议下载模板');
        }
        return false;
    }

    // 读文件
    function read(file) {
        const reader = new FileReader();
        reader.onload = e => {
            const data = new Uint8Array(e.target.result);
            const workbook = XLSX.read(data, {
                type: 'array'
            });
            
            const firstSheetName = workbook.SheetNames[0];
            const sheet = workbook.Sheets[firstSheetName];
            const res = XLSX.utils.sheet_to_json(sheet);
            console.log('json: ', res);
            setList(res);
            
        }
        reader.readAsArrayBuffer(file);
    }

    return (
        <Upload
            beforeUpload={beforeUpload}
            showUploadList={false}
        >
            <Tooltip title="导入将覆盖全部数据">
            <Button type='primary'  size='small' disabled={disabled}>导入</Button>
            </Tooltip>
        </Upload>
    )
}

文件转换如下

if (addDataList.length > 0) {
      const newList = []
      addDataList.forEach(item => {
        let text = item['污染物类型'];
        if (text === '船舶垃圾') text = '0';
        if (text === '生活污水') text = '1';
        if (text === '含油污水') text = '2';
        let text2 = item['作业方式'];
        if (text2 === '垃圾自转运至处置') text2 = '0';
        if (text2 === '污水经管道运输至处置设备') text2 = '1';
        if (text2 === '补录接收') text2 = '2';
        if (text2 === '智能设备接收') text2 = '3';
        if (text2 === '扫车船码接收') text2 = '4';
        if (text2 === '扫码设备接收') text2 = '5';
        if (text2 === '预约接收') text2 = '6';
        if (text2 === '上岸即处理') text2 = '7';
        let text3 = item['转运状态'];
        if (text3 === '已转运') text3 = '0';
        if (text3 === '未转运') text3 = '1';
        if (text3 === '待受理') text3 = '2';
        if (text3 === '待确认') text3 = '3';
        newList.push({
          receiveNumbers: item['单号'],
          shipName: item['船名'],
          type: text,
          quantity: item['数量'],
          receiveCompany: item['接收单位'],
          taskCompany: item['作业单位'],
          taskTime: item['接收开始时间'],
          taskMode: text2,
          state: text3
        })
      })
      let newLists = newList.filter((item) => {
        return JSON.stringify(item) != "{}";
      });
      const params = id.split('-');
          updateDetails({
            taskFid: params[0],
            taskType: params[1],
            code: 1,
            data: {
              status: '1',
              list: [...newLists, ...list1, ...list2],
            }
          }).then(res => {
            if (res.code === '0') {
              message.success('上传成功')
              getList();
              handleRefresh();
            }
          })
    }
  }, [addDataList]);

验证规则:船名是否为空;数量是否为数值;接收单位是否为空;作 业单位是否为空;作 业时间格式是否为:“2021-07-22 18:07:00”,且是否属于对应周内;作 业方式是否为“垃圾自转运至处置”、“污水经管道运输至处置设备”、“污水直排市政管道”、“码头补录”、“智能设备接收”、“扫车船码接收”、“扫码设备接收”、“预约接收”;状态是否为“已转运”、“未转运”、“待受理”、“待确认”。

  • 写回答

1条回答 默认 最新

  • 崽崽的谷雨 2022-04-20 11:00
    关注

    那你首先 读取 文件 ,然后 解析 出 数据 ,数据 要做验证 那你需要 遍历数据 ,对数据项 用正则 或者其它规则 判断 不符合 就提示

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 4月20日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘