ant design pro 上传组件 手动添加文件信息

<Upload
                  name="file"
                  action="/fscserv/fileserv/fileUpload"
                  onChange={onChange}
                  beforeUpload={beforeUpload}
                  accept=".jpg,.png,.txt,.doc,.xlsx,.rar,.zip,.docx,.xls,.ppt,.voc,.mp3,.pdf"
                  maxCount={10}
                  fileList={oldFileList}//设置历史的值
              >
                <Button icon={<UploadOutlined />}>上传附件</Button>
              </Upload>

在Form中使用这个组件。

设置默认历史值的时候智能用fileList,要是使用defaultfilelist则只能设置一次,之后就无法改变了。使用filelist可以来回改变

const [oldFileList, setOldFileList] = useState([]);
if(files.length > 0){
    files.map((item)=>{
        let temp = {
            uid:item.attachId,
            name:item.attachName,
            status:'success',
            // url:item.attachPath,
        };
        fff.push(temp);
    })
}
fff为对象的数组
setOldFileList([...fff]);

 

因为使用了fileList,所以在onchange上传文件的时候,需要有一个地方特殊处理

 const  onChange = (info) => {
        setOldFileList(info.fileList.slice());//如果在upload中fileList中使用了,上传时,要使用这个
        console.log('onChange=',info);
        if (info.file.status !== 'uploading') {
            console.log("uploading",info.file, info.fileList);
        }
        if (info.file.status === 'done') {
            console.log("done info.file",info.file);
            console.log("done info.file.response",info.file.response.data);
            message.success(`${info.file.name} 文件上传成功。`);
            setFileList(info.fileList)
        } else if (info.file.status === 'error') {
            message.error(`${info.file.name} 文件上传失败。`);
        }
    };

 

setOldFileList(info.fileList.slice());//如果在upload中fileList中使用了,上传时,要使用这个。比较关键

https://ant.design/components/upload-cn/

https://github.com/ant-design/ant-design/issues/2423

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值