Upload上传
文件上传
何时使用
需要上传文件时。
代码演示
文件上传。
import { Upload } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['.deb', '.txt', '.pdf', 'image/*'],
uploadImmediately: true,
showUploadList: false,
};
ReactDOM.render(
<div>
<Upload {...props} />
</div>,
mountNode,
);
选择文件后,点击按钮上传。
import { Upload } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['.deb', '.txt', '.pdf', 'image/*'],
uploadImmediately: false,
};
ReactDOM.render(
<div>
<Upload {...props} />
</div>,
mountNode
);
限制同时上传文件的数量。
import { Upload } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['.deb', '.txt', '.pdf', 'image/*'],
uploadImmediately: false,
fileListMaxLength: 2,
};
ReactDOM.render(
<div>
<Upload {...props} />
</div>,
mountNode
);
可以自定义上传请求得到响应后的处理方式,包括成功和失败。这将覆盖默认的处理行为。
import { Upload, message } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['.deb', '.txt', '.pdf', 'image/*'],
uploadImmediately: false,
onUploadSuccess: response => message.success(response),
onUploadError: (error, response, file) => console.log(error, response, file),
};
ReactDOM.render(
<div>
<Upload {...props} />
</div>,
mountNode
);
为一个文件上传请求追加一个FormData
实例。请查看开发者工具中的Network标签。
import { Upload } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['.deb', '.txt', '.pdf', 'image/*'],
uploadImmediately: false,
data: {
key1: 'value1',
key2: 'value2',
},
onUploadSuccess: response => console.log(response),
};
ReactDOM.render(
<div>
<Upload {...props} />
</div>,
mountNode
);
在组件右上角显示提示信息,如接受的文件类型等。
import { Upload } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['image/*'],
uploadImmediately: false,
extra: <p>请上传图片文件(jpg, jpeg, png...)</p>,
onUploadSuccess: response => console.log(response),
};
ReactDOM.render(
<div>
<Upload {...props} />
</div>,
mountNode
);
直接调用组件的上传方法,可以使用showUploadBtn={false}
使自带的上传按钮消失。
import { Upload, Button, Form, Output } from 'choerodon-ui/pro';
const props = {
headers: {
'Access-Control-Allow-Origin': '*',
},
action: '//localhost:3000/upload',
multiple: true,
accept: ['.deb', '.txt', '.pdf', 'image/*'],
uploadImmediately: false,
showUploadBtn: false,
showPreviewImage: true,
};
class Demo extends React.Component {
upload;
saveUpload = node => (this.upload = node);
handleBtnClick = () => {
this.upload.startUpload();
};
render() {
return (
<Form header="文件管理">
<Output label="选择Logo" renderer={() => <Upload ref={this.saveUpload} {...props} />} />
<Button style={{ marginBottom: 10 }} color="primary" onClick={this.handleBtnClick}>
提交
</Button>
</Form>
);
}
}
ReactDOM.render(<Demo />, mountNode);
API
Upload 属性说明如下:
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
accept | 接受上传的文件类型 input accept Attribute | string | |
action | 上传的地址 | string | '' |
data | 上传所需参数或返回上传参数的方法 | object|fucntion(file) | |
headers | 设置上传的请求头部,IE10 以上有效 | object | |
multiple | 是否支持多选,IE10 以上有效 | boolean | false |
name | <input> 元素的 name 字段 | string | 'file' |
uploadImmediately | 是否选择文件后立即上传(若为否,组件自身会提供上传按钮) | boolean | true |
fileListMaxLength | 同时上传文件的最大数量 | number | 0 ,表示无限制 |
showPreviewImage | 是否显示图片预览 | boolean | true |
showUploadBtn | 是否显示上传按钮 | boolean | true |
showUploadList | 是否显示上传列表 | boolean | true |
previewImageWidth | 预览图片 Img 元素的宽度 | number | 100 |
onFileChange | input 元素内部文件变化的回调 | (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 类型接口:
export interface UploadFile {
uid: string;
size: number;
name: string;
filename?: string;
lastModified?: string;
lastModifiedDate?: Date;
url?: string;
status?: 'error' | 'success' | 'uploading' | 'done';
percent?: number;
thumbUrl?: string;
originFileObj?: File;
response?: any;
error?: any;
linkProps?: any;
type: string;
msClose?: boolean;
}