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.它的功能很强大.但是我没有配置成可以跨域上传的. ...
随机推荐
- Linux打包和压缩——管理打包和压缩的命令
Linux打包和压缩——管理打包和压缩的命令 摘要:本文主要学习了Linux的打包命令和压缩命令. tar命令 tar命令可以用来进行打包和解打包,压缩和解压缩. 基本语法 打包和压缩的语法: tar ...
- Java面向对象——三大特性
Java面向对象——三大特性 摘要:本文主要介绍了面型对象的三大特性. 封装 什么是封装 封装,就是指一个类隐藏了对象的属性和实现细节,对自己的数据和方法进行访问权限控制,只允许某些类和对象进行访问和 ...
- http协议历史
HTTP 通常被译为超文本传输协议, 但这种译法并不严谨. 严谨的译名应该为“超文本转移协议”. 最初设想的基本理念是: 借助多文档之间相互关联形成的超文本( HyperText) , 连成可相互参阅 ...
- 大白话说GIT常用操作,常用指令git操作大全
列一下在开发中用的比较多的git指令 git clone https://github.com/chineseLiao/Small-career // 克隆远程仓库到本地 git add . // 把 ...
- Struts2 OGNL表达式、ValueStack
OGNL简介 OGNL,即Object-Graph Navigation Language,对象视图导航语言,是一种数据访问语言,比EL表达式更加强大: EL只能从11个内置对象中取值,且只能获取属性 ...
- linux时间校对
在虚拟机中查看安装的linux时间,总是不对,于是查阅资料,有很多解决办法,我个人比较倾向于用ntpdate的方式更新时间. 感觉比较好用的一个方法: 输入date查看时间,发现不对,相差甚远!(当前 ...
- 【微信错误】{"errcode":"40013","errmsg":"invalid appid hint: [mackRA06203114]","success":false}
一.异常背景 发送可以跳转小程序的公众号模版消息 二.原因 当前公众号没有和被跳转的小程序关联 三.解决办法 去公众号平台将小程序和公众号进行关联就可以了
- odoo10学习笔记十一:视图综述
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189322.html 一:视图标签等公共结构 name (必选) 用于通过名字查找标签 model: 与v ...
- Navicat Premium 详解
Navicat是一套数据库管理工具,专为简化数据库的管理及降低系统管理成本而设. Navicat 是以直觉化的图形用户界面而建的,可以安全和简单地创建.组织.访问并共用信息. Navicat Pre ...
- django + pycharm 开局
1. 首先有 python3 2. 安装了pycharm 3. 配置开局 下面是用的全局的解释器,如果是用的虚拟环境的,Existing interpreter 选择虚拟环境的解释器. 4. set ...