简体   繁体   English

HTML5和JavaScript:如何使粘贴处理完全/更异步?

[英]HTML5 and JavaScript: How do I make paste handling fully/more asynchronous?

I'm implementing copy-paste functionality into a browser application using the ClipboardData API, as explained in this answer. 我正在使用ClipboardData API将复制粘贴功能实现到浏览器应用程序中, 如本答案中所述。

The FileReader::readAsDataURL(blob) provides asynchronous reading of the file data, which is great. FileReader::readAsDataURL(blob)提供了对文件数据的异步读取,这很棒。

var items = (event.clipboardData || event.originalEvent.clipboardData).items;
var reader = new FileReader();
reader.onload = function(event){
   /*add item (i.e. image) to page*/}; //callback

var blob = items[0].getAsFile(); //not async
reader.readAsDataURL(blob); //async

Questions: 问题:
1) Is there a way to make the DataTransferItem::getAsFile() method asynchronous? 1)有没有办法使DataTransferItem::getAsFile()方法异步?
2) Is there a way to get FileReader to take a DataTransferItem as an argument so it can do the async itself like it does already with blobs? 2)有没有办法让FileReaderDataTransferItem作为一个参数,这样它可以像blob一样执行async本身?
3) Am I out of luck? 3)我运气不好吗?

Is there a way to make the DataTransferItem::getAsFile() method asynchronous? 有没有办法使DataTransferItem :: getAsFile()方法异步?

No. It's specified as synchronous. 不, 它被指定为同步。 However, there is absolutely no reason to make this asynchronous - a reference to the File is quickly constructed and does not incorporate any data shoveling. 但是,绝对没有理由使这种异步 - 对文件的引用很快被构造,并且不包含任何数据挖掘。 Just as input.files does synchronously get you references to not-yet-read-in files. 就像input.files同步地获取对尚未读入的文件的引用一样。

Is there a way to get FileReader to take a DataTransferItem as an argument so it can do the async itself like it does already with blobs? 有没有办法让FileReaderDataTransferItem作为一个参数,以便它可以像blob一样执行异步本身?

No. It just takes the item.getAsFile() blob, and can read that in asynchronously. 不。它只需要item.getAsFile() blob,并且可以异步读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM