.
FileReader.readAsDataURL()
, , base64, .
MIME- , ( Blob type
, , , ), .
const fr = new FileReader();
const fakeGif = new Blob(['foo'], {type:'image/gif'});
fr.onload = onload;
fr.readAsDataURL(fakeGif);
function onload(){
console.log(fr.result);
console.log(atob(fr.result.split(',')[1]))
}
Hide result
, , Safari - DataTransferItem, .
, Grab png , Safari tiff.
dev, , /tiff.
, DataTransferItem, png, , , Safari .
, TIFF png - :
var files = [];
if (!window.DataTransferItem) {
var pasteCatcher = document.createElement("div");
pasteCatcher.setAttribute("contenteditable", "");
pasteCatcher.style.opacity = 0.5;
document.body.appendChild(pasteCatcher);
pasteCatcher.focus();
document.addEventListener("click", function() {
pasteCatcher.focus();
});
}
window.addEventListener("paste", pasteHandler);
function pasteHandler(e) {
if (e.clipboardData) {
var items = e.clipboardData.items || e.clipboardData.files;
itemcount = items.length;
if (itemcount) {
for (var i = 0; i < items.length; i++) {
getItem(items[i]);
}
} else {
setTimeout(checkInput, 1);
}
} else {
console.log("checking input");
setTimeout(checkInput, 1);
}
}
function getItem(item) {
if (item.type.indexOf("image") !== -1) {
var blob = item instanceof Blob ? item : item.getAsFile();
files.push(blob);
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
createImage(source);
}
}
function checkInput() {
var child = pasteCatcher.childNodes[0];
if (child) {
if (child.tagName === "IMG") {
getPngFromIMG(child, function(blob) {
pasteCatcher.innerHTML = "";
files.push(blob);
createImage(URL.createObjectURL(blob));
});
}
}
}
function getPngFromIMG(img, callback) {
if (img.naturalWidth)
draw();
else
img.onload = draw;
function draw() {
var canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
canvas.getContext('2d').drawImage(img, 0, 0);
canvas.toBlob(callback);
}
}
function createImage(source) {
var pastedImage = new Image();
pastedImage.onload = function(e) {
loadImage.src = e.target.src;
}
pastedImage.src = source;
}
btn.onclick = function() {
console.log(files);
}
<textarea id="pasteArea" placeholder="Paste Image Here"></textarea>
<img id="loadImage" />
<button id="btn">do something with pasted files</button>
Hide result