我找到了一种获取仅适用于 Google chrome 的图像的方法。我正忙于开发一个也适用于 Firefox 的版本。
以灵感形式:http ://strd6.com/2011/09/html5-javascript-pasting-image-data-in-chrome/
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="jquery.js" type="text/javascript"></script>
<script>
// Created by STRd6
// MIT License
// jquery.paste_image_reader.js
(function() {
(function($) {
var defaults;
$.event.fix = (function(originalFix) {
return function(event) {
event = originalFix.apply(this, arguments);
if (event.type.indexOf('copy') === 0 || event.type.indexOf('paste') === 0) {
event.clipboardData = event.originalEvent.clipboardData;
}
return event;
};
})($.event.fix);
defaults = {
callback: $.noop,
matchType: /image.*/
};
return $.fn.pasteImageReader = function(options) {
if (typeof options === "function") {
options = {
callback: options
};
}
options = $.extend({}, defaults, options);
return this.each(function() {
var $this, element;
element = this;
$this = $(this);
return $this.bind('paste', function(event) {
var clipboardData, found;
found = false;
clipboardData = event.clipboardData;
return Array.prototype.forEach.call(clipboardData.types, function(type, i) {
var file, reader;
if (found) {
return;
}
if (type.match(options.matchType) || clipboardData.items[i].type.match(options.matchType)) {
file = clipboardData.items[i].getAsFile();
reader = new FileReader();
reader.onload = function(evt) {
return options.callback.call(element, {
dataURL: evt.target.result,
event: evt,
file: file,
name: file.name
});
};
reader.readAsDataURL(file);
return found = true;
}
});
});
});
};
})(jQuery);
}).call(this);
jQuery("html").pasteImageReader(function(results) {
var dataURL, filename;
filename = results.filename, dataURL = results.dataURL;
jQuery('#deze').html('<img src="' + dataURL + '" />');
});
</script>
<style>
#deze{
height:400px;
width:400px;
border:2px solid red;
}
</style>
</head>
<body>
<div id="deze">testdiv</div>
</body>
</html>
笔记:
在这个例子中,创建者覆盖了 $ sing,你不能再像这样使用 jquery: $('#deze')