自定义上传组件,只需要在内部的值变化之后调用props中的onChange方法就可以托管在From组件中,

此外为了保证,初始化值发生变化后组件也发生变化,需要检测initialValue 变化,这是定义了

  1. checkInitialValue 方法,在render的时候调用
  1. import React from 'react';
  2. import { Upload, message, Button, Icon } from 'antd';
  3. import { upload, search } from '@/services/upload';
  4.  
  5. class UploadFile extends React.PureComponent {
  6. constructor() {
  7. super();
  8. this.getfiletimeout = null;
  9. this.state = { fileIds: [], fileList: [] };
  10. this.isEmpty = true;
  11. }
  12.  
  13. queryFileIds = idstr => {
  14. const self = this;
  15.  
  16. if (idstr && idstr.split) {
  17. const ids = idstr.split(',');
  18. this.isEmpty = false;
  19. ids.forEach(id => {
  20. self.queryFileId(id);
  21. });
  22. } else if (!this.isEmpty) {
  23. this.setEmpty();
  24. }
  25. };
  26.  
  27. queryFileId = id => {
  28. const { fileIds } = this.state;
  29. if (id && fileIds.indexOf(id) < 0) {
  30. // fileIds.push(id);
  31. this.getFile2(id);
  32. this.fid = id;
  33. }
  34. };
  35.  
  36. getFile2 = id => {
  37. const self = this;
  38. search({ id: id }).then(res => {
  39. if (res && res.success && res.data && res.data.length > 0) {
  40. const ff = self.dbInfoToFileInfo(res.data[0]);
  41. ff.status = 'done';
  42. self.addFile(ff);
  43. }
  44. });
  45. // clearTimeout(self.getfiletimeout);
  46. // self.getfiletimeout = null;
  47. };
  48.  
  49. addFile = file => {
  50. const { fileList = [], fileIds = [] } = this.state;
  51. if (fileIds.indexOf(file.id) < 0) {
  52. fileIds.push(file.id);
  53. const newFiles = [...fileList, file];
  54. // this.setState({ fileList: newFiles });
  55. this.updateValue(newFiles);
  56. }
  57. };
  58.  
  59. removeFile = file => {
  60. const { fileList = [] } = this.state;
  61. const newFiles = [];
  62. const newIds = [];
  63. fileList.forEach(file1 => {
  64. if (file1.id !== file.id) {
  65. newFiles.push(file1);
  66. newIds.push(file1.id);
  67. }
  68. });
  69. this.updateValue(newFiles, newIds);
  70. };
  71.  
  72. setEmpty = () => {
  73. this.isEmpty = true;
  74. // this.setState({ fileList: [], fileIds: [] });
  75. this.updateValue([])
  76. };
  77.  
  78. updateValue = (fileList = []) => {
  79. const { onChange } = this.props;
  80. const ids = fileList.map(file => file.id);
  81. onChange(ids.join());
  82. this.setState({ fileList: fileList, fileIds: ids });
  83. };
  84.  
  85. dbInfoToFileInfo = (d = {}) => {
  86. const f = {};
  87. f.name = d.fileName;
  88. f.uid = d.id;
  89. f.type = d.fileType;
  90. f.id = d.id;
  91. f.url = `/springboot/attachment/get?id=${d.id}`;
  92. return f;
  93. };
  94.  
  95. checkInitialValue = () => {
  96. try {
  97. const v = this.props['data-__meta'].initialValue;
  98. if (v !== this.initialValue) {
  99. this.props.onChange(v);
  100. }
  101. this.initialValue = v;
  102. } catch (e) {
  103. // const msg = e;
  104. console.log(e);
  105. }
  106. };
  107.  
  108. upload = ({
  109. file,
  110. filename,
  111. // headers,
  112. // onError,
  113. // onProgress,
  114. onSuccess,
  115. // withCredentials,
  116. }) => {
  117. const self = this;
  118. const p = {};
  119. p[filename] = file;
  120.  
  121. upload(p).then(res => {
  122. const info = {
  123. file: {
  124. status: 'done',
  125. },
  126. fileList: [],
  127. };
  128. if (res && res.success && res.data && res.data.length > 0) {
  129. const ff = self.dbInfoToFileInfo(res.data[0]);
  130. ff.status = 'done';
  131. self.addFile(ff);
  132. info.file = ff;
  133. // onChange(res.data[0].id);
  134. } else {
  135. info.file.status = 'error';
  136. }
  137.  
  138. onSuccess(info);
  139. });
  140. };
  141.  
  142. render() {
  143. const self = this;
  144. const { value, maxSize = 10, text = '点击上传文件' } = this.props;
  145. console.log(value);
  146. this.checkInitialValue();
  147. const { fileList } = this.state;
  148. const upprops = {
  149. name: 'file',
  150. headers: {
  151. authorization: 'authorization-text',
  152. },
  153. customRequest: self.upload,
  154. onChange(info) {
  155. if (info.file.status !== 'uploading') {
  156. console.log(info.file, info.fileList);
  157. }
  158. if (info.file.status === 'removed') {
  159. self.removeFile(info.file);
  160. }
  161. if (info.file.status === 'done') {
  162. message.success(`${info.file.name} 上传成功`);
  163. } else if (info.file.status === 'error') {
  164. message.error(`${info.file.name} 上传失败.`);
  165. }
  166. },
  167. };
  168.  
  169. this.queryFileIds(value);
  170.  
  171. return (
  172. <Upload {...upprops} fileList={fileList} disabled={maxSize <= fileList.length}>
  173. <Button>
  174. <Icon type="upload" />
  175. {text}
  176. </Button>
  177. </Upload>
  178. );
  179. }
  180. }
  181.  
  182. export default UploadFile;

使用自定义上传组件

  1. const TemplateFileIdItem = props => {
  2. const { data, form, style } = props;
  3. console.log(data.templateFileId)
  4. return (
  5. <FormItem
  6. labelCol={{ span: 5 }}
  7. wrapperCol={{ span: 15 }}
  8. style={{ ...style }}
  9. label="模板文件"
  10. >
  11. {form.getFieldDecorator('templateFileId', {
  12. rules: [{ required: false, message: '请输入副标题' }],
  13. initialValue: data.templateFileId,
  14. })(<FileUpload placeholder="请输入" autoComplete="off" maxSize={1} />)}
  15. </FormItem>
  16. );
  17. };

ant-design自定义FormItem--上传文件组件的更多相关文章

  1. ant design for vue 上传文件

    1.使用customRequest customRequest 通过覆盖默认的上传行为,可以自定义自己的上传实现 Function 定义customRequest,之前定义action行为会被覆盖,可 ...

  2. element-ui上传组件,通过自定义请求上传文件

    记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...

  3. layui上传文件组件(前后端代码实现)

    我个人博客系统上传特色图片功能就是用layui上传文件组件做的.另外采用某个生态框架,尽量都统一用该生态框架对应的解决方案,因为这样一来,有这么几个好处?1.统一而不杂糅,有利于制定相应的编码规范,方 ...

  4. Django和Ueditor自定义存储上传文件的文件名

    django台后默认上传文件名 在不使用分布式文件存储系统等第三方文件存储时,django使用默认的后台ImageField和FileField上传文件名默认使用原文件名,当出现同名时会在后面追加下随 ...

  5. 基于element ui 实现七牛云自定义key上传文件,并监听更新上传进度

    借助上传Upload 上传组件的 http-request 覆盖默认的上传行为,可以自定义上传的实现 <el-upload multiple ref="sliderUpload&quo ...

  6. Element ui 上传文件组件(单文件上传) 点击提交 没反应

    element ui 第一次上传文件后 上传其他文件再次点击不再次提交 需要使用 clearFiles 清空已上传文件列表 这时候在次点击 上传按钮 就会惊喜的发现 可以上传了使用方法 this.$r ...

  7. [自动运维]ant脚本打包,上传文件到指定服务器,并部署

    1.根节点使用,表示根目录为当前目录,默认启动的target为build,项目名称为othersysm, <project basedir="." default=" ...

  8. JS组件系列——自己封装一个上传文件组件

    页面调用: $('#fileUpload').cemsUpload({ errorEmpty:'<s:text name="cupgrade.view.tip.upload.file. ...

  9. django 自定义存储上传文件的文件名

    一.需求: Django实现自定义文件名存储文件 使文件名看起来统一 避免收到中文文件导致传输.存储等问题 相同的文件也需要使用不同的文件名 二.实现思路: 思路: 生成14位随机字母加数字.后10位 ...

  10. Angular4 后台管理系统搭建(10) - 做一个通用的可跨域上传文件的组件

    写的很慢,不知不觉这是第十篇了.但是我其他事情太多,只能抽空写下.现在angular4或angular2流行的上传方式是ng2-file-upload.它的功能很强大.但是我没有配置成可以跨域上传的. ...

随机推荐

  1. yii2 HeadersAlreadySentException 报错

    An Error occurred while handling another error:exception 'yii\web\HeadersAlreadySentException' with ...

  2. STP生成树理解

    1.STP的功能 a. 防止二层环路    b .实现网络冗余备份 2.STP的选择机制 目的:  确定阻塞的端口 STP 交换机的角色: 根交换机,非根交换机 STP的选票:     BPDU Ro ...

  3. Kibana插件开发

    当前开发环境 Kibana版本:7.2 elasticsearch版本:7.2 开发环境安装可参考:https://github.com/elastic/kibana/blob/master/CONT ...

  4. 【IPHONE开发-OBJECTC入门学习】复制对象,深浅复制

    转自:http://blog.csdn.net/java886o/article/details/9046273 #import <Foundation/Foundation.h> int ...

  5. Telegram Android源码问题汇总 持续更新

    libtgvoip目录为空 git clone下来的工程中带有submodule时,submodule的内容没有下载下来,执行如下命令 cd Telegram git submodule update ...

  6. 关于使用Hadoop MR的Eclipse插件开发时遇到Permission denied问题的解决办法【转】

    搭建了一个Hadoop的环境,Hadoop集群环境部署在几个Linux服务器上,现在想使用windows上的Java客户端来操作集群中的HDFS文件,但是在客户端运行时出现了如下的认证错误,被折磨了几 ...

  7. [PHP] 最简单的权限控制设计

    假设url部分我们只有action和method , 某个控制器下的某个方法 , 比如:log/loginlog   查看日志下的登陆日志, action就是log , method就是loginlo ...

  8. java 获取安全随机字符

    private static final char[] CHAR_32 = new char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', ...

  9. JS高阶---作用域面试

    面试题1: ,答案为10 有一点需要明确:作用域是在定义编写代码时已经决定好的 面试题2: 结果1: 结果2: 首先在内部作用域找,没有 然后在全局作用域找,window没有,所以会报错 如果想找对象 ...

  10. c# 第六节 c#的程序结构,以及vs的文件结构

    本节内容: 1:c#的程序结构 2:深入了解vs的文件 1:c#的程序结构 实例: 2:深入了解vs的文件 三者的关系: 3:命令空间是什么 使用别名: