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.它的功能很强大.但是我没有配置成可以跨域上传的. ...
随机推荐
- yii2 HeadersAlreadySentException 报错
An Error occurred while handling another error:exception 'yii\web\HeadersAlreadySentException' with ...
- STP生成树理解
1.STP的功能 a. 防止二层环路 b .实现网络冗余备份 2.STP的选择机制 目的: 确定阻塞的端口 STP 交换机的角色: 根交换机,非根交换机 STP的选票: BPDU Ro ...
- Kibana插件开发
当前开发环境 Kibana版本:7.2 elasticsearch版本:7.2 开发环境安装可参考:https://github.com/elastic/kibana/blob/master/CONT ...
- 【IPHONE开发-OBJECTC入门学习】复制对象,深浅复制
转自:http://blog.csdn.net/java886o/article/details/9046273 #import <Foundation/Foundation.h> int ...
- Telegram Android源码问题汇总 持续更新
libtgvoip目录为空 git clone下来的工程中带有submodule时,submodule的内容没有下载下来,执行如下命令 cd Telegram git submodule update ...
- 关于使用Hadoop MR的Eclipse插件开发时遇到Permission denied问题的解决办法【转】
搭建了一个Hadoop的环境,Hadoop集群环境部署在几个Linux服务器上,现在想使用windows上的Java客户端来操作集群中的HDFS文件,但是在客户端运行时出现了如下的认证错误,被折磨了几 ...
- [PHP] 最简单的权限控制设计
假设url部分我们只有action和method , 某个控制器下的某个方法 , 比如:log/loginlog 查看日志下的登陆日志, action就是log , method就是loginlo ...
- java 获取安全随机字符
private static final char[] CHAR_32 = new char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', ...
- JS高阶---作用域面试
面试题1: ,答案为10 有一点需要明确:作用域是在定义编写代码时已经决定好的 面试题2: 结果1: 结果2: 首先在内部作用域找,没有 然后在全局作用域找,window没有,所以会报错 如果想找对象 ...
- c# 第六节 c#的程序结构,以及vs的文件结构
本节内容: 1:c#的程序结构 2:深入了解vs的文件 1:c#的程序结构 实例: 2:深入了解vs的文件 三者的关系: 3:命令空间是什么 使用别名: