mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-30 12:28:33 +08:00
7085ab39d0
* type: improve type assert * type: improve type assert * chore: modify the path
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import React from 'react';
|
|
import { UploadOutlined } from '@ant-design/icons';
|
|
import type { UploadProps } from 'antd';
|
|
import { Button, Upload } from 'antd';
|
|
|
|
const props: UploadProps = {
|
|
action: 'https://660d2bd96ddfa2943b33731c.mockapi.io/api/upload',
|
|
listType: 'picture',
|
|
beforeUpload(file) {
|
|
return new Promise((resolve) => {
|
|
const reader = new FileReader();
|
|
reader.readAsDataURL(file);
|
|
reader.onload = () => {
|
|
const img = document.createElement('img');
|
|
img.src = reader.result as string;
|
|
img.onload = () => {
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = img.naturalWidth;
|
|
canvas.height = img.naturalHeight;
|
|
const ctx = canvas.getContext('2d')!;
|
|
ctx.drawImage(img, 0, 0);
|
|
ctx.fillStyle = 'red';
|
|
ctx.textBaseline = 'middle';
|
|
ctx.font = '33px Arial';
|
|
ctx.fillText('Ant Design', 20, 20);
|
|
canvas.toBlob((result) => resolve(result as Blob));
|
|
};
|
|
};
|
|
});
|
|
},
|
|
};
|
|
|
|
const App: React.FC = () => (
|
|
<Upload {...props}>
|
|
<Button icon={<UploadOutlined />}>Upload</Button>
|
|
</Upload>
|
|
);
|
|
|
|
export default App;
|