一、Antdupload的概述
Antdupload是一个基于React框架的上传组件。它使用了Ant Design的风格,并提供了丰富的上传功能。在使用Antdupload之前,我们需要先安装Ant Design和React。
二、Antdupload的使用
1、引入Antdupload和Ant Design样式
import { Upload } from 'antd';
import 'antd/dist/antd.css';
2、构造上传组件
const props = {
  name: 'file',
  action: '/upload.do',
  headers: {
    authorization: 'authorization-text',
  },
  onChange(info) {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      message.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      message.error(`${info.file.name} file upload failed.`);
    }
  },
};
ReactDOM.render(
  
    
  ,
  mountNode,
);
3、参数说明:
action: 上传地址,必选参数
data: 上传所需数据或上传函数,可选参数
defaultFileList: 默认已上传的文件列表。数组中的每个对象均需包含 uid 属性
directory: 支持上传文件夹,IE10+ 以及 Edge 浏览器不支持该参数
headers: 设置上传的请求头部
listType: 上传列表的内建样式,支持三种基本样式 text, picture 和 picture-card
multiple: 是否支持多选文件
name: 发到后台的文件参数名
showUploadList: 显示已上传文件列表的样式
三、Antdupload的新特性
1、Chunk Upload
Antdupload支持大文件上传,以及将大文件拆分为多个块同时上传,从而提高上传速度和稳定性。这样做的好处是,如果某一块上传失败,只需要重新上传该块即可,不需要重新上传整个文件。
import { Upload } from 'antd';
import 'antd/dist/antd.css';
const props = {
  name: 'file',
  action: '/upload.do',
  headers: {
    authorization: 'authorization-text',
  },
  onChange(info) {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      message.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      message.error(`${info.file.name} file upload failed.`);
    }
  },
  data: {
    chunkCount: 4,
  },
  listType: 'picture',
  multiple: true,
};
ReactDOM.render(
  
              
    Click or drag file to this area to upload
    Support for a single or bulk upload.
  ,
  mountNode,
);2、Image Preview
Antdupload支持图片预览功能,同时支持自定义预览和删除操作。
import { Upload, Modal } from 'antd';
import 'antd/dist/antd.css';const { confirm } = Modal;
function getBase64(file) {
 return new Promise((resolve, reject) => {
 const reader = new FileReader();
 reader.readAsDataURL(file);
 reader.onload = () => resolve(reader.result);
 reader.onerror = error => reject(error);
 });
}
class PicturesWall extends React.Component {
 state = {
 previewVisible: false,
 previewImage: '',
 fileList: [
 {
 uid: '-1',
 name: 'image.png',
 status: 'done',
 url: 'https://gw.alipayobjects.com/zos/rmsportal/uMfMFlvUuceEyPpotzlq.png',
 },
 ],
 };
 handleCancel = () => this.setState({ previewVisible: false });
 handlePreview = async file => {
 if (!file.url && !file.preview) {
 file.preview = await getBase64(file.originFileObj);
 }
 this.setState({
 previewImage: file.url || file.preview,
 previewVisible: true,
 });
 };
 handleChange = ({ fileList }) => this.setState({ fileList });
 handleRemove = file => {
 confirm({
 title: 'Delete this file?',
 onOk() {
 console.log('OK');
 },
 onCancel() {
 console.log('Cancel');
 },
 });
 };
 render() {
 const { previewVisible, previewImage, fileList } = this.state;
 const uploadButton = (
 Upload
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/244338.html
 
 微信扫一扫
微信扫一扫  支付宝扫一扫
支付宝扫一扫 