react批量上传

该文章介绍了如何在React前端和Node.js后端实现Excel文件的批量上传。后端使用Node.js处理文件上传,解析Excel数据并存储到数据库,前端则借助antd库进行文件上传,上传成功或失败会有相应的反馈提示。
摘要由CSDN通过智能技术生成

react批量上传



一、后端接口(node)

router.post("/excelUpload", (req, res) => {
    const form = new multiparty.Form()   //multiparty需要导入
    form.uploadDir = "upload"
    form.parse(req, async (err, formData, excelData) => {
        let dataUrl = excelData.file[0].path
        let data = xlsx.parse(dataUrl)[0].data    //xlsx需要导入
        let brr = []  //存放数据的数组
        data.forEach(item => {
            brr.push({     //这里是相关的字段
                "name": item[0],
                "depart": item[1],
                "starttime": item[2],
                "endtime": item[3],
                "desclist": item[2]
            })
        })
        brr.forEach(async(item)=>{
            await peoplegoModel.create(item)    //peopleModel是数据模型,
                                                //create加入数据库中
        })
    })
})

二、react前端

代码如下(示例):

<Upload {...props}>
            <Button icon={<UploadOutlined />}>批量上传</Button>
          </Upload>
  const props = {
    name: 'file',
    action: 'http://127.0.0.1:7777/excelUpload',
    headers: {
      authorization: 'authorization-text',
    },
    onChange(info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === 'done') {
        message.success(`${info.file.name} file uploaded successfully`);
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name} file upload failed.`);
      }
    },
  };

总结

文件上传antd里的代码直接复制,只需要改接口即可,excel里面的数值要和数据库中的字段对应

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值