Upload 上传

文件选择上传和拖拽上传控件。

何时使用

上传是将信息(网页、文字、图片、视频等)通过网页或者上传工具发布到远程服务器上的过程。

  • 当需要上传一个或一些文件时。
  • 当需要展现上传的进度时。
  • 当需要使用拖拽交互时。

代码演示

基本使用

文件上传。

Upload上传 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload } from 'choerodon-ui/pro';
  4. const props = {
  5. headers: {
  6. 'Access-Control-Allow-Origin': '*',
  7. },
  8. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  9. multiple: true,
  10. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  11. uploadImmediately: true,
  12. showUploadList: false,
  13. };
  14. ReactDOM.render(
  15. <div>
  16. <Upload {...props} />
  17. </div>,
  18. document.getElementById('container'),
  19. );

点击按钮上传

选择文件后,点击按钮上传。

Upload上传 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload } from 'choerodon-ui/pro';
  4. const props = {
  5. headers: {
  6. 'Access-Control-Allow-Origin': '*',
  7. },
  8. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  9. multiple: true,
  10. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  11. uploadImmediately: false,
  12. };
  13. ReactDOM.render(
  14. <div>
  15. <Upload {...props} />
  16. </div>,
  17. document.getElementById('container'),
  18. );

追加上传文件列表

以追加形式上传文件列表

Upload上传 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload } from 'choerodon-ui/pro';
  4. class App extends React.Component {
  5. render() {
  6. const props = {
  7. headers: {
  8. 'Access-Control-Allow-Origin': '*',
  9. },
  10. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  11. multiple: true,
  12. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  13. uploadImmediately: false,
  14. withCredentials: false,
  15. partialUpload: true,
  16. appendUpload: true,
  17. };
  18. return <Upload {...props} />;
  19. }
  20. }
  21. ReactDOM.render(<App />, document.getElementById('container'));

上传文件列表控制

使用 beforeUpload 返回 false 后,控制上传文件列表

Upload上传 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload, message } from 'choerodon-ui/pro';
  4. class App extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. uploadFileList: [
  9. {
  10. name: 'xxx.jpg',
  11. type: 'image/jpg',
  12. status: 'success',
  13. size: 2800,
  14. uid: 1,
  15. url:
  16. 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
  17. },
  18. ],
  19. };
  20. }
  21. handleBefore = (file, fileList) => {
  22. const isJPG = file.type === 'image/jpeg';
  23. if (!isJPG) {
  24. message.error('You can only upload JPG file!');
  25. }
  26. const isLt2M = file.size / 1024 / 1024 < 2;
  27. if (!isLt2M) {
  28. message.error('Image must smaller than 2MB!');
  29. }
  30. if (isJPG && isLt2M) {
  31. this.setState({
  32. uploadFileList: [...this.state.uploadFileList, file],
  33. });

数量限制

限制同时上传文件的数量。

Upload上传 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload, message } from 'choerodon-ui/pro';
  4. const handleBefore = (file, fileList) => {
  5. const isJPG = file.type === 'image/jpeg';
  6. if (!isJPG) {
  7. message.error('You can only upload JPG file!');
  8. }
  9. const isLt2M = file.size / 1024 / 1024 < 2;
  10. if (!isLt2M) {
  11. message.error('Image must smaller than 2MB!');
  12. }
  13. return isJPG && isLt2M;
  14. };
  15. const props = {
  16. headers: {
  17. 'Access-Control-Allow-Origin': '*',
  18. },
  19. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  20. multiple: true,
  21. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  22. uploadImmediately: false,
  23. fileListMaxLength: 2,
  24. beforeUpload: handleBefore,
  25. };

附加提示信息

在组件右上角显示提示信息,如接受的文件类型等。

Upload上传 - 图6

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload } from 'choerodon-ui/pro';
  4. const props = {
  5. headers: {
  6. 'Access-Control-Allow-Origin': '*',
  7. },
  8. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  9. multiple: true,
  10. accept: ['image/*'],
  11. uploadImmediately: false,
  12. extra: <p>请上传图片文件(jpg, jpeg, png...)</p>,
  13. onUploadSuccess: (response) => console.log(response),
  14. };
  15. ReactDOM.render(
  16. <div>
  17. <Upload {...props} />
  18. </div>,
  19. document.getElementById('container'),
  20. );

自定义响应处理方法

可以自定义上传请求得到响应后的处理方式,包括成功和失败。这将覆盖默认的处理行为。

Upload上传 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload, message } from 'choerodon-ui/pro';
  4. const props = {
  5. headers: {
  6. 'Access-Control-Allow-Origin': '*',
  7. },
  8. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  9. multiple: true,
  10. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  11. uploadImmediately: false,
  12. onUploadSuccess: (response) => message.success(response.message),
  13. onUploadError: (error, response, file) => console.log(error, response, file),
  14. };
  15. ReactDOM.render(
  16. <div>
  17. <Upload {...props} />
  18. </div>,
  19. document.getElementById('container'),
  20. );

附加额外数据

为一个文件上传请求追加一个FormData实例。请查看开发者工具中的 Network 标签。

Upload上传 - 图8

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload } from 'choerodon-ui/pro';
  4. const props = {
  5. headers: {
  6. 'Access-Control-Allow-Origin': '*',
  7. },
  8. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  9. multiple: true,
  10. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  11. uploadImmediately: false,
  12. data: {
  13. key1: 'value1',
  14. key2: 'value2',
  15. },
  16. onUploadSuccess: (response) => console.log(response),
  17. };
  18. ReactDOM.render(
  19. <div>
  20. <Upload {...props} />
  21. </div>,
  22. document.getElementById('container'),
  23. );

文件删除回调

使用 onRemoveFile 返回 false 后,不移除

Upload上传 - 图9

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload } from 'choerodon-ui/pro';
  4. class App extends React.Component {
  5. handleRemove = (file) => {
  6. console.log('remove', file);
  7. return false;
  8. };
  9. render() {
  10. const props = {
  11. headers: {
  12. 'Access-Control-Allow-Origin': '*',
  13. },
  14. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  15. multiple: true,
  16. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  17. uploadImmediately: false,
  18. onRemoveFile: this.handleRemove,
  19. };
  20. return <Upload {...props} />;
  21. }
  22. }
  23. ReactDOM.render(<App />, document.getElementById('container'));

上传方法

直接调用组件的上传方法,可以使用showUploadBtn={false}使自带的上传按钮消失。

Upload上传 - 图10

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Upload, Button, Form, Output } from 'choerodon-ui/pro';
  4. const props = {
  5. headers: {
  6. 'Access-Control-Allow-Origin': '*',
  7. },
  8. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  9. multiple: true,
  10. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  11. uploadImmediately: false,
  12. showUploadBtn: false,
  13. showPreviewImage: true,
  14. };
  15. class Demo extends React.Component {
  16. upload;
  17. saveUpload = (node) => (this.upload = node);
  18. handleBtnClick = () => {
  19. this.upload.startUpload();
  20. };
  21. render() {
  22. return (
  23. <Form header="文件管理">
  24. <Output
  25. label="选择Logo"
  26. renderer={() => <Upload ref={this.saveUpload} {...props} />}
  27. />
  28. <Button
  29. style={{ marginBottom: 10 }}
  30. color="primary"

API

Upload 属性说明如下:

属性说明类型默认值
accept接受上传的文件类型 input accept Attributestring
action上传的地址string‘’
data上传所需参数或返回上传参数的方法object | function(file)
headers设置上传的请求头部,IE10 以上有效object
multiple是否支持多选,IE10 以上有效booleanfalse
name<input>元素的 name 字段stringfile
uploadImmediately是否选择文件后立即上传(若为否,组件自身会提供上传按钮)booleantrue
fileListMaxLength同时上传文件的最大数量number0,表示无限制
defaultFileList默认显示的文件列表object[]
uploadFileList显示设定的文件列表(可控)object[]
withCredentials上传请求时是否携带 cookiebooleanfalse
appendUpload是否以追加形式添加文件至列表中booleanfalse
partialUpload只上传文件状态不是success的文件booleantrue
beforeUpload上传文件之前的钩子,参数为上传的文件,可对文件在上传之前进行校验操作若返回 false 则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传。(file, fileList) => boolean | Promise
showPreviewImage是否显示图片预览booleantrue
showUploadBtn是否显示上传按钮booleantrue
showUploadList是否显示上传列表booleantrue
previewImageWidth预览图片 Img 元素的宽度number100
onFileChangeinput 元素内部文件变化的回调(fileList: UploadFile[]) => void
onRemoveFile点击移除文件时的回调,返回值为 false 时不移除。支持返回一个 Promise 对象,Promise 对象 resolve(false) 或 reject 时不移除。function(file): booleanPromise
onUploadProgress上传进度变化的回调(percent: number, file: UploadFile) => void
onUploadSuccess上传成功的回调(response: any, file: UploadFile) => void
onUploadError上传出错的回调(error: Error, response: any, file: UploadFile) => void

更多属性请参考 ViewComponent

UploadFile 类型接口:

  1. export interface UploadFile {
  2. uid: string;
  3. size: number;
  4. name: string;
  5. filename?: string;
  6. lastModified?: string;
  7. lastModifiedDate?: Date;
  8. url?: string;
  9. status?: 'error' | 'success' | 'uploading' | 'done';
  10. percent?: number;
  11. thumbUrl?: string;
  12. originFileObj?: File;
  13. response?: any;
  14. error?: any;
  15. linkProps?: any;
  16. type: string;
  17. msClose?: boolean;
  18. }