ant-design自定义FormItem--上传文件组件
自定义上传组件,只需要在内部的值变化之后调用props中的onChange方法就可以托管在From组件中,
此外为了保证,初始化值发生变化后组件也发生变化,需要检测initialValue 变化,这是定义了
checkInitialValue 方法,在render的时候调用
import React from 'react';
import { Upload, message, Button, Icon } from 'antd';
import { upload, search } from '@/services/upload'; class UploadFile extends React.PureComponent {
constructor() {
super();
this.getfiletimeout = null;
this.state = { fileIds: [], fileList: [] };
this.isEmpty = true;
} queryFileIds = idstr => {
const self = this; if (idstr && idstr.split) {
const ids = idstr.split(',');
this.isEmpty = false;
ids.forEach(id => {
self.queryFileId(id);
});
} else if (!this.isEmpty) {
this.setEmpty();
}
}; queryFileId = id => {
const { fileIds } = this.state;
if (id && fileIds.indexOf(id) < 0) {
// fileIds.push(id);
this.getFile2(id);
this.fid = id;
}
}; getFile2 = id => {
const self = this;
search({ id: id }).then(res => {
if (res && res.success && res.data && res.data.length > 0) {
const ff = self.dbInfoToFileInfo(res.data[0]);
ff.status = 'done';
self.addFile(ff);
}
});
// clearTimeout(self.getfiletimeout);
// self.getfiletimeout = null;
}; addFile = file => {
const { fileList = [], fileIds = [] } = this.state;
if (fileIds.indexOf(file.id) < 0) {
fileIds.push(file.id);
const newFiles = [...fileList, file];
// this.setState({ fileList: newFiles });
this.updateValue(newFiles);
}
}; removeFile = file => {
const { fileList = [] } = this.state;
const newFiles = [];
const newIds = [];
fileList.forEach(file1 => {
if (file1.id !== file.id) {
newFiles.push(file1);
newIds.push(file1.id);
}
});
this.updateValue(newFiles, newIds);
}; setEmpty = () => {
this.isEmpty = true;
// this.setState({ fileList: [], fileIds: [] });
this.updateValue([])
}; updateValue = (fileList = []) => {
const { onChange } = this.props;
const ids = fileList.map(file => file.id);
onChange(ids.join());
this.setState({ fileList: fileList, fileIds: ids });
}; dbInfoToFileInfo = (d = {}) => {
const f = {};
f.name = d.fileName;
f.uid = d.id;
f.type = d.fileType;
f.id = d.id;
f.url = `/springboot/attachment/get?id=${d.id}`;
return f;
}; checkInitialValue = () => {
try {
const v = this.props['data-__meta'].initialValue;
if (v !== this.initialValue) {
this.props.onChange(v);
}
this.initialValue = v;
} catch (e) {
// const msg = e;
console.log(e);
}
}; upload = ({
file,
filename,
// headers,
// onError,
// onProgress,
onSuccess,
// withCredentials,
}) => {
const self = this;
const p = {};
p[filename] = file; upload(p).then(res => {
const info = {
file: {
status: 'done',
},
fileList: [],
};
if (res && res.success && res.data && res.data.length > 0) {
const ff = self.dbInfoToFileInfo(res.data[0]);
ff.status = 'done';
self.addFile(ff);
info.file = ff;
// onChange(res.data[0].id);
} else {
info.file.status = 'error';
} onSuccess(info);
});
}; render() {
const self = this;
const { value, maxSize = 10, text = '点击上传文件' } = this.props;
console.log(value);
this.checkInitialValue();
const { fileList } = this.state;
const upprops = {
name: 'file',
headers: {
authorization: 'authorization-text',
},
customRequest: self.upload,
onChange(info) {
if (info.file.status !== 'uploading') {
console.log(info.file, info.fileList);
}
if (info.file.status === 'removed') {
self.removeFile(info.file);
}
if (info.file.status === 'done') {
message.success(`${info.file.name} 上传成功`);
} else if (info.file.status === 'error') {
message.error(`${info.file.name} 上传失败.`);
}
},
}; this.queryFileIds(value); return (
<Upload {...upprops} fileList={fileList} disabled={maxSize <= fileList.length}>
<Button>
<Icon type="upload" />
{text}
</Button>
</Upload>
);
}
} export default UploadFile;
使用自定义上传组件
const TemplateFileIdItem = props => {
const { data, form, style } = props;
console.log(data.templateFileId)
return (
<FormItem
labelCol={{ span: 5 }}
wrapperCol={{ span: 15 }}
style={{ ...style }}
label="模板文件"
>
{form.getFieldDecorator('templateFileId', {
rules: [{ required: false, message: '请输入副标题' }],
initialValue: data.templateFileId,
})(<FileUpload placeholder="请输入" autoComplete="off" maxSize={1} />)}
</FormItem>
);
};
ant-design自定义FormItem--上传文件组件的更多相关文章
- ant design for vue 上传文件
1.使用customRequest customRequest 通过覆盖默认的上传行为,可以自定义自己的上传实现 Function 定义customRequest,之前定义action行为会被覆盖,可 ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
- layui上传文件组件(前后端代码实现)
我个人博客系统上传特色图片功能就是用layui上传文件组件做的.另外采用某个生态框架,尽量都统一用该生态框架对应的解决方案,因为这样一来,有这么几个好处?1.统一而不杂糅,有利于制定相应的编码规范,方 ...
- Django和Ueditor自定义存储上传文件的文件名
django台后默认上传文件名 在不使用分布式文件存储系统等第三方文件存储时,django使用默认的后台ImageField和FileField上传文件名默认使用原文件名,当出现同名时会在后面追加下随 ...
- 基于element ui 实现七牛云自定义key上传文件,并监听更新上传进度
借助上传Upload 上传组件的 http-request 覆盖默认的上传行为,可以自定义上传的实现 <el-upload multiple ref="sliderUpload&quo ...
- Element ui 上传文件组件(单文件上传) 点击提交 没反应
element ui 第一次上传文件后 上传其他文件再次点击不再次提交 需要使用 clearFiles 清空已上传文件列表 这时候在次点击 上传按钮 就会惊喜的发现 可以上传了使用方法 this.$r ...
- [自动运维]ant脚本打包,上传文件到指定服务器,并部署
1.根节点使用,表示根目录为当前目录,默认启动的target为build,项目名称为othersysm, <project basedir="." default=" ...
- JS组件系列——自己封装一个上传文件组件
页面调用: $('#fileUpload').cemsUpload({ errorEmpty:'<s:text name="cupgrade.view.tip.upload.file. ...
- django 自定义存储上传文件的文件名
一.需求: Django实现自定义文件名存储文件 使文件名看起来统一 避免收到中文文件导致传输.存储等问题 相同的文件也需要使用不同的文件名 二.实现思路: 思路: 生成14位随机字母加数字.后10位 ...
- Angular4 后台管理系统搭建(10) - 做一个通用的可跨域上传文件的组件
写的很慢,不知不觉这是第十篇了.但是我其他事情太多,只能抽空写下.现在angular4或angular2流行的上传方式是ng2-file-upload.它的功能很强大.但是我没有配置成可以跨域上传的. ...
随机推荐
- CPU体系结构(组成部分)
在准备网络工程师考试,里面有些知识点是比较常考的.自己写这篇博客呢,当作是笔记吧,自己看一看也分享给大家一起学习. 这部分的内容就是讲CPU里面的组成结构以及各部分的功能. CPU的构成:CPU主要由 ...
- ZKEACMS 无法运行问题汇总
前言 如果你还不知道ZKEACMS,不妨先了解一下. ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来 官方地址:http://www.zkea.net/zkeacms ...
- 【前端_css】RGB 常用颜色列表
转载博客:RGB 常用颜色列表
- Django框架(二十一)--Django rest_framework-频率组件
一.作用 为了控制用户对某个url请求的频率,比如,一分钟以内,只能访问三次 二.自定义频率类 # 写一个频率认证类 class MyThrottle: visit_dic = {} visit_ti ...
- FCOS及其和Faster R-CNN的区别
RetinaNet,SSD,YOLOv3,Faster R-CNN等都是Anchor-based的检测器,即需要预定义的Anchor boxes来进行训练.FCOS是一种Anchor-free和Pro ...
- 虚拟机安装苹果macOS系统
Windows10系统虚拟机vmware15上装macos系统 一.简要步骤: 1.准备软件. 2.关闭VMware服务: 3.解压unlocker,运行文件: 4.启动vmware,选择macOS镜 ...
- IDEA优秀插件分享之---Mybatis Log Plugin
小伙伴们在使用mybatis的时候有时候会出现一些sql异常,这个时候就需要对执行的sql语句进行检查.然而mybatis一般使用log4j打印执行的sql语句,类型下面这种的: 这个时候如果sql语 ...
- 如果使用jsp文件,需要在配置文件中配置resources项,才能让idea识别这个jsp文件
没有添加这一项在编译后的.class文件中的结构目录是这样子的 添加上这一个配置项,在class配置文件中的位置是这样子的: 添加的配置文件是这样子的: <resources> <r ...
- halcon笔记1
* 获得二值图 Image_binread_image(Image, 'C:/Alex/halcon/test.bmp') // 读图 threshold (Image, Regions, , ) / ...
- Linux安装docker(ubuntu16.04和centos7.4)
ubuntu16.04版本 1.安装依赖 sudo apt-get install apt-transport-https ca-certificates software-properties-co ...