9

I'm trying to add an image paste function to my web application, using the standard routine:

$('textarea').on('paste', function (ev) {
    var clipboardData = ev.originalEvent.clipboardData;

    $.each(clipboardData.items, function (i, item) {
        if (item.type.indexOf("image") !== -1) {
            var reader = new FileReader();

            reader.readAsDataURL(item.getAsFile());
            reader.addEventListener('loadend', ...);
            ...
        }
    });
});

The full sample can be found here: http://jsfiddle.net/t8t2zj6k/

It works correctly when I copy & paste an image from an image viewer software, but when I'm trying to do the same thing using a file browser (e.g. Finder on Mac or Nautilus on Linux) as a result I get only a text string with the file path or even an image with file type icon instead of an original file.

Is there any way to handle pastes from a file browser properly?

4

2 回答 2

3

似乎可能是 Chrome 的问题?我在 Safari 或 Firefox 中看不到任何内容。http://code.google.com/p/chromium/issues/detail?id=361980

于 2015-04-19T00:31:59.630 回答
1

也许您想将图像粘贴为data:

去看看HTML5 将图像粘贴到页面

于 2017-09-12T07:51:45.390 回答