Upload上传

文件上传

何时使用

需要上传文件时。

代码演示

Upload 上传 - 图1

基本使用

文件上传。

  1. import { Upload } from 'choerodon-ui/pro';
  2. const props = {
  3. headers: {
  4. 'Access-Control-Allow-Origin': '*',
  5. },
  6. action: '//localhost:3000/upload',
  7. multiple: true,
  8. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  9. uploadImmediately: true,
  10. showUploadList: false,
  11. };
  12. ReactDOM.render(
  13. <div>
  14. <Upload {...props} />
  15. </div>,
  16. mountNode,
  17. );

Upload 上传 - 图2

点击按钮上传

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

  1. import { Upload } from 'choerodon-ui/pro';
  2. const props = {
  3. headers: {
  4. 'Access-Control-Allow-Origin': '*',
  5. },
  6. action: '//localhost:3000/upload',
  7. multiple: true,
  8. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  9. uploadImmediately: false,
  10. };
  11. ReactDOM.render(
  12. <div>
  13. <Upload {...props} />
  14. </div>,
  15. mountNode
  16. );

Upload 上传 - 图3

数量限制

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

  1. import { Upload } from 'choerodon-ui/pro';
  2. const props = {
  3. headers: {
  4. 'Access-Control-Allow-Origin': '*',
  5. },
  6. action: '//localhost:3000/upload',
  7. multiple: true,
  8. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  9. uploadImmediately: false,
  10. fileListMaxLength: 2,
  11. };
  12. ReactDOM.render(
  13. <div>
  14. <Upload {...props} />
  15. </div>,
  16. mountNode
  17. );

Upload 上传 - 图4

自定义响应处理方法

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

  1. import { Upload, message } from 'choerodon-ui/pro';
  2. const props = {
  3. headers: {
  4. 'Access-Control-Allow-Origin': '*',
  5. },
  6. action: '//localhost:3000/upload',
  7. multiple: true,
  8. accept: ['.deb', '.txt', '.pdf', 'image/*'],
  9. uploadImmediately: false,
  10. onUploadSuccess: response => message.success(response),
  11. onUploadError: (error, response, file) => console.log(error, response, file),
  12. };
  13. ReactDOM.render(
  14. <div>
  15. <Upload {...props} />
  16. </div>,
  17. mountNode
  18. );

Upload 上传 - 图5

附加额外数据

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

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

Upload 上传 - 图6

附加提示信息

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

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

Upload 上传 - 图7

上传方法

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

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

API

Upload 属性说明如下:

属性说明类型默认值
accept接受上传的文件类型 input accept Attributestring
action上传的地址string''
data上传所需参数或返回上传参数的方法object|fucntion(file)
headers设置上传的请求头部,IE10 以上有效object
multiple是否支持多选,IE10 以上有效booleanfalse
name<input>元素的 name 字段string'file'
uploadImmediately是否选择文件后立即上传(若为否,组件自身会提供上传按钮)booleantrue
fileListMaxLength同时上传文件的最大数量number0,表示无限制
showPreviewImage是否显示图片预览booleantrue
showUploadBtn是否显示上传按钮booleantrue
showUploadList是否显示上传列表booleantrue
previewImageWidth预览图片 Img 元素的宽度number100
onFileChangeinput 元素内部文件变化的回调(fileList: UploadFile[]) => void
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. }