New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ant design upload 上传图片支持粘贴上传 #2
Comments
参考用法const handleClickPaste = async () => {
try {
const read = await (navigator.clipboard as any).read(); // 此 API 较新,类型不完善(typescript4.4- 无此方法)
const foundImageMimeType = read[0].types.find((val: string) => val.startsWith('image/png'));
if (foundImageMimeType) {
const blob = await read[0].getType(foundImageMimeType);
// 截止 Chrome98 / Safari15,仍然不支持本地文件上传
const file = new File([blob], 'screenshot.png', {
type: foundImageMimeType,
});
manuallyUpload(file); // => 上传文件并添加到 fileList
} else {
message.info('剪切板中没有 png 类型图片');
}
} catch (e) {
const err = e as Error;
let errTips = '';
if (err.name === 'NotAllowedError') {
errTips = '您没有授权使用剪切板,Chrome 用户请刷新按钮右边的「锁」形 icon 打开权限';
} else if (err.name === 'DataError') {
errTips = '不支持读取剪切板中的图片文件,请按 Ctrl+V';
} else {
errTips = '浏览器可能不支持读取剪切板,请手动上传';
}
message.error(`${errTips} (${err.message})`, 5);
}
}; <p>
<span>同时支持拖拽文件到虚线框内,或者在页面上按 Ctrl+V</span>
{(navigator?.clipboard as any)?.read && (
<>
<span>,也可以 </span>
<span onClick={handleClickPaste}>
从剪切板上传截图
</span>
<Tooltip color="gray" title="首次使用时,需要点击同意授权。仅支持微信等工具的截图!">
<QuestionCircleOutlined />
</Tooltip>
</>
)}
</p> 👆🏻 当然这里也可以用自定义 ContextMenu 组件(右键菜单)。 API 限制
总结虽然勉强算是能用,但是吧……永远不要相信用户对报错的理解能力。与其冒这个风险,拖拽上传 / Ctrl+V 不香吗。 |
您好,我想请问一下,ant-design的Upload,这种方式的粘贴上次似乎还无法做到Upload组件显示上传进度。感觉需要Upload组件提供一个调用上传的api才可以。 |
你这个实现跟ant design一点关系也没有啊,这样自定义上传就无法触发上传进度等交互行为,和点击上传的表现行为不一致 |
Sorry guys, I don't speak chainese, but I do speak typescript :) In my solution I dispatch drop event in Input of antd Upload First of all we have to access to the hidden <input type="file" .../> of antd. They uses rc-upload, where uploader is an private method and they don't have any methods to get uploader. We can use "backdoor" of typescript to override it interface:
This ref should be passed to antd component but typescript will not like it, this why we'll pass it as
now we can go to the most interesting part, first of all we have to handle past event, to do so I used my reusable custom hook declared somewhere in project:
Then we tell our component with antd Upload to listen to this event:
All the magic happening in handlePasteFiles function which looks like:
I think my solution is better, because you can avoid using function createFileObject and don't create antd file object manually. |
@BogdanGorelkin Wow, that's so cool, it works! |
背景
本文为了解决ant design upload组件不提供粘贴方式上传图片而生。该组件只支持打开文件夹选取图片以及拖拽图片的方式上传图片,这两种方式可以适应大部分上传图片的场景。但是当用户不想保存图片到文件夹而且不方便拖拽图片的时候,通过
复制-粘贴
上传图片的便捷性就体现出来了。经测试,复制/拷贝方式包括:
局限性: 不能双击弹出粘贴选项(因为不是输入框),只能按command+v 触发图片上传。
效果图
步骤
1、监听paste事件
2、获取粘贴板内容:e.clipboardData.items
在chrome 控制台把该对象打印出来,如下:
注意: 这是一个类数组对象!不是数组。不能用
map
和forEach
遍历,但是可以用for...of
遍历,因为其内部实现了iterator接口
。如果该对象的kind是file,则把图片读出来。代码如下:
3、把文件读出来后,上传至服务器
代码如下:
4、生成缩略图
上传成功后返回的key和hash(两者的值一样,用于图片唯一标识),图片地址格式:http://baseUrl/${key}
另外上传成功后可以生成缩略图(base64编码)显示在前端(需要借助原始file对象,不是图片url)
代码如下:
5、接入antd upload组件中,使之和其他方式上传图片的效果一致
fileObject
是一个存储图片详细信息的对象,下面是生成图片对象的方法,传入三个参数,如下:为什么是这个格式?因为这是antd upload 组件fileList数组中对象的格式,fileList是上传的图片列表(数组)。可以在onchange发生后打印出来看看
代码如下:
将此对象push进upload组件的fileList数组后就可以接入ant design upload中
代码如下:
The text was updated successfully, but these errors were encountered: