我复制了一个大约一百万行的 Excel 表格。当我查看系统上的剪贴板时,它似乎包含大约 250MB 的数据。但是,我only需要从中获取样式信息,例如:
整个数据的大小(远)不到 1MB。有没有办法像读取文件或流一样读取剪贴板,以便我可以执行以下操作:
clipboard.read(1024)
否则,如果我做直线:
evt.clipboardData.getData('text/html')
并抓取我想要的数据部分after获取数据,我花了10多秒的时间!然而我相信如果我能够以部分方式读取剪贴板数据(就像它是一个文件一样),该事件应该只需要 0.1 秒左右。
我在这里能做什么?是否可以使用FileReader
在剪贴板上?如果是这样,那该怎么办呢?
The Clipboard.read https://w3c.github.io/clipboard-apis/#dom-clipboard-read注释中引用的 API 可以将剪贴板内容作为列表返回ClipboardItem https://w3c.github.io/clipboard-apis/#clipboarditem对象,然后您可以从中获取Blob https://w3c.github.io/FileAPI/#dfn-Blob对象,然后您可以.slice https://w3c.github.io/FileAPI/#dfn-slice执行部分读取。
const perm = await navigator.permissions.query({ name: 'clipboard-read' });
switch (perm.state) {
case 'granted':
case 'prompt':
break;
default:
throw new Error("clipboard-read permission not granted");
}
const items = await navigator.clipboard.read();
for (const item of items) {
const blob = await item.getType('text/html');
const first1M = await blob.slice(0, 1048576).arrayBuffer();
/* process first1M */
}
然而,剪贴板 API 到目前为止还远未普及。 Firefox ESR 78.9 没有实现它。 (我还没有尝试过其他浏览器;也许在 Chrome 中它已经可以使用了。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)