问题
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