Skip to content

Instantly share code, notes, and snippets.

@ahmadmarafa
Created August 24, 2019 12:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahmadmarafa/b204a8d1e49212d7d26cb0cfb985a798 to your computer and use it in GitHub Desktop.
Save ahmadmarafa/b204a8d1e49212d7d26cb0cfb985a798 to your computer and use it in GitHub Desktop.
antd fileupload on form submit
import React, { Component } from 'react'
import { Upload, Form, Icon } from "antd";
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 5 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 18 },
md: { span: 16 },
lg: { span: 12 },
},
};
String.prototype.ucFirst = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
}
export default class FileUpload extends Component {
constructor(props) {
super(props);
this.state = {
files: props.fileList || [],
fileList: []
}
}
dummyRequest = ({ file, onSuccess }, multiple, limit) => {
const reader = new FileReader();
reader.onloadend = () => {
this.setState({
files: [
...this.state["files"],
{
uid: (new Date()).getMilliseconds(),
name: file.name,
status: 'done',
url: reader.result,
file: file,
}
].slice(0, multiple ? ( limit > -1 ? limit : this.state.files.length + 1) : 1 )
})
}
if (file) {
reader.readAsDataURL(file);
}
setTimeout(() => {
onSuccess("ok");
}, 0);
};
normFile = (element , multiple , limit) => {
if(!multiple) {
return element.file ;
}
if (element.file.status === "removed") {
this.setState({
fileList: this.state.fileList.filter((v) => "file" in element.file && v.uid != element.file.file.uid),
files: this.state.files.filter((v) => v.status !== 'removed')
});
} else {
this.setState({
fileList: [...this.state.fileList, element.file]
});
}
return this.state.fileList.slice(0 , ( limit > -1 ? limit : this.state.fileList.length + 1));
}
render() {
const { multiple = true, element, required = true, accept = "image/*", decorator, limit = -1 } = this.props;
const label = element.split("_").join(" ").ucFirst();
return (
<Form.Item {...formItemLayout} label={label} required={required} id={element} >{
decorator(element, {
valuePropName: element,
getValueFromEvent: (f) => this.normFile(f , multiple , limit ) ,
})(
<Upload multiple={multiple}
accept={accept}
listType="picture-card"
fileList={this.state.files}
customRequest={(f) => this.dummyRequest(f, multiple, limit)}
>
<div>
<Icon type="plus" />
<div className="ant-upload-text">Upload</div>
</div>
</Upload>
)
}</Form.Item>
)
}
}
@Nxtra
Copy link

Nxtra commented Oct 5, 2020

This looks very nice. Do you have it embedded in codepen / stackblitz so I can play with it?

@Nxtra
Copy link

Nxtra commented Oct 5, 2020

How should we pass the decorator into this component?

@ahmadmarafa
Copy link
Author

ahmadmarafa commented Oct 5, 2020

Hi Nxtra :) ,
just send decorator as prop , <FileUpload fileList={this.state.list} element='images' decorator={getFieldDecorator} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment