mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-14 16:19:15 +08:00
7fd093bd0a
* docs: add general components TS demo * docs: add layout components TS demo * docs: add navigation components TS demo * docs: add data entry components TS demo * chore(deps): add types for qs * docs: add data display TS demo * docs: add feedback components TS demo * docs: add other components TS demo * chore(deps): add types * docs: unified demo code style * docs: fix lint error * docs: add demo TS type * docs: fix demo TS type * test: update snapshot * docs: fix TS demo * feat: update Rate character type * docs: fix lint error * feat: update Rate character type * feat: update Rate character type
85 lines
1.9 KiB
Markdown
85 lines
1.9 KiB
Markdown
---
|
|
order: 7
|
|
title:
|
|
zh-CN: 手动上传
|
|
en-US: Upload manually
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
`beforeUpload` 返回 `false` 后,手动上传文件。
|
|
|
|
## en-US
|
|
|
|
Upload files manually after `beforeUpload` returns `false`.
|
|
|
|
```tsx
|
|
import React, { useState } from 'react';
|
|
import { Upload, Button, message } from 'antd';
|
|
import { UploadOutlined } from '@ant-design/icons';
|
|
import type { RcFile, UploadFile, UploadProps } from 'antd/es/upload/interface';
|
|
|
|
const App: React.FC = () => {
|
|
const [fileList, setFileList] = useState<UploadFile[]>([]);
|
|
const [uploading, setUploading] = useState(false);
|
|
|
|
const handleUpload = () => {
|
|
const formData = new FormData();
|
|
fileList.forEach(file => {
|
|
formData.append('files[]', file.originFileObj as RcFile);
|
|
});
|
|
setUploading(true);
|
|
// You can use any AJAX library you like
|
|
fetch('https://www.mocky.io/v2/5cc8019d300000980a055e76', {
|
|
method: 'POST',
|
|
body: formData,
|
|
})
|
|
.then(res => res.json())
|
|
.then(() => {
|
|
setFileList([]);
|
|
message.success('upload successfully.');
|
|
})
|
|
.catch(() => {
|
|
message.error('upload failed.');
|
|
})
|
|
.finally(() => {
|
|
setUploading(false);
|
|
});
|
|
};
|
|
|
|
const props: UploadProps = {
|
|
onRemove: file => {
|
|
const index = fileList.indexOf(file);
|
|
const newFileList = fileList.slice();
|
|
newFileList.splice(index, 1);
|
|
setFileList(newFileList);
|
|
},
|
|
beforeUpload: file => {
|
|
setFileList([...fileList, file]);
|
|
|
|
return false;
|
|
},
|
|
fileList,
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Upload {...props}>
|
|
<Button icon={<UploadOutlined />}>Select File</Button>
|
|
</Upload>
|
|
<Button
|
|
type="primary"
|
|
onClick={handleUpload}
|
|
disabled={fileList.length === 0}
|
|
loading={uploading}
|
|
style={{ marginTop: 16 }}
|
|
>
|
|
{uploading ? 'Uploading' : 'Start Upload'}
|
|
</Button>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default App;
|
|
```
|