How to read a file on paste event in HTML5

半城伤御伤魂 提交于 2019-12-20 11:52:33

问题


In HTML5 we know that we can read files using predefined API. As of now I have tried

<input type="file"> and the File Drop method. They have worked fine for me.

But I want to know the possibility of pasting a file on a div and capturing the file on paste. For Example

 $('#dummyDIV').bind('paste',function()
 {
      // Like var file = files[0]
 });

Thanks


回答1:


You can only read the file name of pasted file.

http://jsfiddle.net/vdNFR/

$('body').bind('paste', function(a, b, c) {
    console.log(a.originalEvent.clipboardData);
    console.log(a.originalEvent.clipboardData.getData('File'));
    console.log(a.originalEvent.clipboardData.getData('Text'));
    if (a.originalEvent.clipboardData.files[0]) console.log(a.originalEvent.clipboardData.files[0].getAsFile());
    if (a.originalEvent.clipboardData.items[0]) console.log(a.originalEvent.clipboardData.items[0].getAsFile());
    console.log(a, b, c);
});

It would be a major security hole if any browser would allow this situation to happen.



来源:https://stackoverflow.com/questions/8578136/how-to-read-a-file-on-paste-event-in-html5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!