1

我正在获取浏览器的打印屏幕并将其粘贴到可内容编辑的 div 中。我得到了图像,但我想以 kb 为单位找出图像大小,但在 Firefox 中找不到。

if (!window.Clipboard) {
  var pasteCatcher = document.createElement("div");
  pasteCatcher.setAttribute("contenteditable", "");
  pasteCatcher.style.opacity = 0;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;
      if (items) {
     for (var i = 0; i < items.length; i++) {
        if (items[i].type.indexOf("image") !== -1) {
           var blob = items[i].getAsFile();
           var size_blob = (blob.size * 3) / 4;
           var blob_size=bytesToSize(size_blob,2);        

           var URLObj = window.URL || window.webkitURL;
           var source = URLObj.createObjectURL(blob);

        var reader = new FileReader();
            reader.onload = function(event){
                createImage(event.target.result); 
            };
             reader.readAsDataURL(blob);
        }
     }
  }
  } else {  
  setTimeout(checkInput, 1);
  }
  }
  function checkInput() {
     var child = pasteCatcher.childNodes[0];
     pasteCatcher.innerHTML = "";

  if (child) {
     if (child.tagName === "IMG") {

     createImage(child.src);
     }
  }
 }
 function createImage(source) {
   var pastedImage = new Image("300","300");
   pastedImage.onload = function() {
   }
    pastedImage.src = source;
    var arrayBuffer = source.split(",");
    var size_blob = (source.length * 3)/ 4;
    var blob_size=bytesToSize(size_blob,2);
    document.getElementById('image2').appendChild(pastedImage);
 }

请你在这里帮忙

4

0 回答 0