2

I am trying to save a image/screenshot through paste Ctrl+V into a div #pasteImageDiv by user. Its working fine with Chrome but not working with IE.

I am using IE10.

Until now, what I have been able to find is that if I paste any text to div #pasteImageDiv, it catch the onpaste event properly, even in IE. But if I paste an image instead of text, it even doesn't catch onpaste (IE doesn't even enter into the function that handles the onpaste event).

document.getElementById('pasteImageDiv').onpaste = function (event) {

It is working well in Chrome whether I paste a text string or an image. I hope you understand what kind of issue I am facing. Still, if any additional information is needed, please let me know.

 $('#pasteImageHere, #pasteImageDiv').click(function(e){ //on paste image button click
   e.preventDefault();
   $('#hideOnPaste').hide();
   //document.getElementById('pasteImageDiv').click();
  document.getElementById('pasteImageDiv').style.backgroundColor = "#F1F1F1";
  document.getElementById('pasteImageDiv').onpaste = function (event) {
    $('#hideOnPaste').hide();
    //console.log(event.clipboardData.getData('image/png'));
     // use event.originalEvent.clipboard for newer chrome versions
     var items = (event.clipboardData  || event.originalEvent.clipboardData).items;
     console.log(JSON.stringify(items)); // will give you the mime types
     // find pasted image among pasted items
     var blob = null;
     for (var i = 0; i < items.length; i++) {
    if (items[i].type.indexOf("image") === 0) {
      blob = items[i].getAsFile();
    }
     }
     // load image if there is a pasted image
     if (blob !== null) {
    var reader = new FileReader();
    reader.onload = function(event) {
      console.log(event.target.result); // data url!
      var elem = document.createElement("img");
      elem.setAttribute("id", "pastedImage");
      elem.setAttribute("height", "200");
      elem.setAttribute("width", "300");
      document.getElementById("pasteImageDiv").appendChild(elem);
      document.getElementById("pastedImage").src = event.target.result;
      document.getElementById('inputImageData').value = event.target.result;
      console.log($('#inputImageData').val());
      $('#pastedImage').css('width', '300px');
      $('#pastedImage').css('height', '200px');
      document.getElementById("pastedImage").style.height = '200px';
    };
    reader.readAsDataURL(blob);
    $('#removePastedImage').show();
     }
     
   }
 
  });
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
sssurii
  • 750
  • 4
  • 17
  • 1
    IE10 just doesn't support it AFAIK – A. Wolff Jun 17 '16 at 15:50
  • It also seems to me like this is not basic enough to give to IE and hope it works. – Jeremy Thille Jun 17 '16 at 15:52
  • 1
    I assume you're talking contenteditable here - if not ignore me. I've just tried it and Ctrl-V works for me with IE7-11. Edge and Chrome etc only accept Ctrl-V for pastes *within* the document - to paste external copies you need to right-click and select Paste from the context menu (if available). – Tony Duffill Jun 17 '16 at 17:09
  • I got it worked for Edge and with little trick for IE 11. still its not working in IE 10. – sssurii Jun 23 '16 at 09:29

1 Answers1

1

Image paste support was added in IE11:

Starting with IE11, images pasted from the clipboard are base64 encoded by default. Users can now easily and safely copy and paste images from their local file system into contenteditable regions of a website. Prior to IE11, pasting a local image on a live website (across security zones) resulted in a broken image icon, as a security measure to prevent local file access.

IE11 is the first browser that supports both pasting images directly from clipboard (for example, from photo editing software or from PrintScreen) and pasting HTML that incorporates local images (for example, from applications such as Microsoft Office that store images temporarily in local paths). Either DataURI or Blob can be used to encode these images.

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265