- 如何从用户的剪贴板中获取文件
在写这个答案之前,我已经尝试了很多事情,比如从new File('file:///asdasd.jpg')
读取,为<input type='file' />
提供默认值,发送获取请求并获取blob。
这些解决方案似乎都不起作用(Chrome-2021年6月14日(
处理此问题的简单方法:
- 收听文档上的
paste
事件 - 提取文件
- 从该
fileList
创建数组
document.addEventListener('paste', (event) => {
const clipboardData = (event.clipboardData || window.clipboardData);
const files = Array.from(clipboardData.files);
if (files.length > 0) {
// Do something with `files`
}
});