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.它的功能很强大.但是我没有配置成可以跨域上传的. ...
随机推荐
- Spring Boot 使用 Log4j2 & Logback 输出日志到 EKL
文章目录 1.ELK 介绍 2.环境.软件准备 3.ELK 环境搭建 4.Spring Boot 配置示例 4.1.Log4j2 方式配置 4.2.Logback 方式配置 1.ELK 介绍 ELK ...
- Docker(一) - CentOS7中安装Docker - (视频教程)
Docker的使用越来越多,安装也相对简单.本文使用视频的方式展示在CentOS7系统中安装Docker,本文更适合于准备入门学习Docker的童靴. 以下视频,请带上耳机开始聆听 (双击全屏播放) ...
- GitBash上传项目出现[fatal: remote origin already exists.]问题解决方案
问题截图如下: 当出现这个问题时,不要慌,只需要输入 git remote rm origin 就可以解决,输入完前面的命令后再次 输入 git remote add origin https://. ...
- Windows 下 pycharm 创建Django 项目【用虚拟环境的解释器】
1. 背景 我在 Windows 下的 pycharm 直接创建 全新 Django 项目 会 pip 和其他报错 ,暂时解决不了,另外后续的多个项目只需要一套python 环境, 所以可以 ...
- 操作Excel模块openpyxl
安装 pip install openpyxl 想要在文件中插入图片文件,需要安装pillow font(字体类):字号.字体颜色.下划线等 fill(填充类):颜色等 border(边框类):设置单 ...
- CRT&EXCRT学习笔记
非扩展 用于求解线性同余方程组 ,其中模数两两互质 . 先来看一看两个显然的定理: 1.若 x \(\equiv\) 0 (mod p) 且 y \(\equiv\) 0 (mod p) ,则有 x+ ...
- 2019 AI 100 Startups
- WebSocket协议-原理篇
本篇文章主要讲述以下几点: WebSocket的原理与机制 WebSocket与Socket.io WebSocket兼容性 WebSocket的原理与机制 WebSocket协议分为两部分:握手和数 ...
- 在WEB显示实时视频流
转载自:https://www.jianshu.com/p/7ef5490fbef7 安装摄像头 这里使用的是树莓派的官方摄像头,使用普通的 USB 摄像头也可以,但前提是你能够搞的定它的驱动. 大概 ...
- 迪杰斯特拉算法完整代码(Java)
package com.rao.graph; import java.util.*; /** * @author Srao * @className Dijkstra * @date 2019/12/ ...