ant react 上传_React实战之Ant Design—Upload上传_附件上传

React实战之Ant Design—Upload上传_附件上传

Upload组件大家都在官方文档中看过了,但写的时候还是会遇到许多问题,一些新手看了文档后感觉无从下手,本文过多的简绍就不说了,直接看代码和注释,直接用就行

我直接放在form表单中,因为实战中单个附件上传很少几,乎都在form表单中加入一个附件上传所以为了更好的应用,我直接就放在form表单中应用

import React, { PureComponent } from 'react';

import {Form, Button, Icon,Upload} from 'antd';

const FormItem = Form.Item;

@Form.create()

class xxx extends PureComponent {

state = {

fileList: [],//存放上传信息:比如路径、文件名

imgList: [],//存放回显信息:修改时的路径、文件名

};

//form表单提交事件

handleSubmit = e => {

const { dispatch, form } = this.props;

e.preventDefault();

form.validateFieldsAndScroll((err, values) => {

if (!err) {

const { imgList } = this.state

values.imgList = JSON.stringify(imgList);

console.log('values', values);

}

});

};

//上传事件

onChange = ({ fileList }) => {

console.log('file', fileList);

let imgList = [];

fileList.map(function (item, key) {

if (item.response && item.response.success) {

console.log('item.response',item.response);

imgList.push({ url: item.response.url, Name: item.response.name });//这的参数具体看上传成功后返回的值,打印的item.response

} else {

//回显

if (item.url) {

//拼接'http:// 如果路径可以直接使用不需要拼接

imgList.push({ url: item.url.replace('http://', ""), Name: item.name });

}

}

});

this.setState({ fileList, imgList });

}

render() {//const {form: { getFieldDecorator, getFieldValue }} = this.props;

const { fileList } = this.state

const props = {

name: 'UploadFile',//name得看接口需求,name与接口需要的name一致

action: 'http://.......',//接口路径

data: { },//接口需要的参数,无参数可以不写

multiple: true,//支持多个文件

showUploadList: true,//展示文件列表

}

return (

fileList={fileList}

onChange={this.onChange}

>

上传附件

,

提交

);

}

export default xxx;

代码可以用于新增功能和编辑功能,如有问题欢迎联系!不到之处多多指教

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值