87,994
社区成员
发帖
与我相关
我的任务
分享import "babel-polyfill";
import React from "react";
import axios from "axios";
import zhCN from "antd/lib/locale-provider/zh_CN";
import moment from "moment";
import "moment/locale/zh-cn";
// ******************************
import API from '../api/api';
import { LocaleProvider, Icon, Button, message, Upload } from "antd";
import "antd/lib/icon/style/css";
import "antd/lib/button/style/css";
import "antd/lib/message/style/css";
import "antd/lib/upload/style/css";
// ******************************
import "../assets/css/file-upload.css";
// 组件-语言设置
moment.locale("zh-cn");
// 组件-文件上传:开始
class FileUploadPhoto extends React.Component {
constructor(props) {
super(props);
this.state = {
fileList: [],
};
this.get = this.get.bind(this);
}
get() {
let urlProp = "?applicantId=" + xxx.applicantId;
let getUrl = API.xxx + urlProp;
if (this.props.page === "xxx") {
getUrl = API.xxx + urlProp;
}
axios.get(getUrl).then((res) => {
if (res.data.code === "1000") {
this.setState({
fileList: [
{
uid: "1",
id: "id-98000",
name: "pho-98000.png",
url: "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png",
},
{
uid: "2",
id: "id-16000",
name: "pho-16000.png",
url: "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png",
},
],
});
}
}).catch((err) => {
console.log(err);
});
}
componentDidMount() {
this.get();
}
// 文件上传前验证:开始
handleBeforeUpload = file => {
let isImg = false;
const isLtSize = file.size / 1024 / 1024 <= 2;
if (file.type === "image/png" || file.type === "image/jpg" || file.type === "image/jpeg") {
isImg = true;
}
if (!isImg) {
message.error("请上传PNG/JPG/JPEG格式图片");
}
else {
if (!isLtSize) {
message.error("图片不能超过2M");
}
}
return isImg && isLtSize;
};
// 文件上传前验证:结束
handleChange = info => {
let fileList = [...info.fileList];
fileList = fileList.filter(file => {
return (file.type === "image/png" || file.type === "image/jpg" || file.type === "image/jpeg") && file.size / 1024 / 1024 < 1;
});
console.log("fileList.length: " + fileList.length);
if (fileList.length > 5) {
message.error(`最多上传五张`);
return false;
}
fileList = fileList.slice(-5);
if (info.file.status !== "uploading") { }
if (info.file.status === "done") {
message.success(`${info.file.name} 上传成功`);
if (info.file.response.code === "1000") {
fileList = fileList.filter(file => {
file = info.file;
file.id = info.file.response.data;
file.uid = info.file.response.data;
return true;
});
}
}
else if (info.file.status === "error") {
message.error(`${info.file.name} 上传失败`);
}
this.setState({ fileList });
};
handleRemove = (file) => {
let uriProp = "?id=" + file.id;
let uri = API.xxx + uriProp;
if (this.props.page === "xxx") {
uri = API.xxx + uriProp;
}
let options = {};
return fetch(uri, options)
.then((response) => {
message.success(`${file.name} 删除成功`);
}).catch((err) => {
console.log(err);
});
};
render() {
let urlProp = "?xxx&applicantId=" + xxx.applicantId + "&type=xxx";
let uploadUrl = API.xxx + urlProp;
if (this.props.page === "xxx") {
uploadUrl = API.xxx + urlProp;
}
const props = {
action: uploadUrl,
beforeUpload: this.handleBeforeUpload,
onChange: this.handleChange,
onRemove: this.handleRemove,
multiple: false,
name: "file",
accept: "image/png,image/jpg,image/jpeg",
className: "module-upload-file",
};
return (
<LocaleProvider locale={zhCN}>
<Upload {...props} fileList={this.state.fileList} >
<Button><Icon type="upload" /> 图片上传</Button>
</Upload>
</LocaleProvider>
);
}
}
// 组件-文件上传:结束
export default FileUploadPhoto;
this.setState({ fileList });