JS Implementation Ctrl + V paste pictures or screenshots

Source: Internet
Author: User

Browser Environment: Google Chrome

1.ctrl+v paste pictures are listening paste time implementation, the copied data are present clipboarddata below, although the print display data length is 0, but still can get the data

2. Print Clipboarddata.items discovery is a datatransferitem.

3.DataTransferItem there is a getasfile () method that can obtain the file

Document.addeventlistener ('Paste', Function (Event) {Console.log (Event); varIschrome =false; if(Event. clipboarddata | |Event. Originalevent) {            //Some versions of Chrome are using Event.originalevent            varClipboarddata = (Event. clipboarddata | |Event. Originalevent.clipboarddata); if(clipboarddata.items) {//For Chrome                varItems =Clipboarddata.items, Len.=Items.length, Blob=NULL; Ischrome=true;  for(vari =0; i < Len; i++) {Console.log (items[i]); if(Items[i].type.indexof ("Image") !== -1) {                        //Getasfile () This method is only living standard Firefox IE11 does not supportBlob =Items[i].getasfile (); }                }            }        }    })
View Code

You can get to the Blob object at this time, you can choose to display on the page, you can also choose to send to the background

3.1 Displaying pictures

3.1.1 Executes the following code, using the Blob object to display

  var bloburl=Url.createobjecturl (BLOB);       document.getElementById ("imgnode"). Src=bloburl;
View Code

3.1.2 Using base64 code display, need to use FileReader

Reader.onload = function (event) {                        //  Event.target.result is the BASE64 encoded string for the picture                        varevent. Target.result;                        document.getElementById ("imgnode"). src=base64_str;                    }                    Reader.readasdataurl (BLOB);
View Code

3.2 Uploading to the background

3.2.1 Generates Formdata, where Formdata is generated and requires the help of a canvas

//with canvas, generate Formdata                    varCanvas = document.createelement ('Canvas'); varDataurl = Canvas.todataurl ('Image/jpeg',0.5); varFD =NewFormData (document.forms[0]); Fd.append ("The_file"Blob'Image.png'); //Create a XMLHttpRequest object                    varXHR =NewXMLHttpRequest (); Xhr.open ('POST','/image' ); Xhr.onload=function () {if(Xhr.readystate = = =4 ) {                            if(Xhr.status = = = $ ) {                                vardata =Json.parse (Xhr.responsetext);                            Console.log (data); } Else{console.log (xhr.statustext);                    }                        };                    }; Xhr.onerror=function (e) {console.log (xhr.statustext); } xhr.send (FD);
View Code

3.3 Full Code

The complete code is as follows

<! DOCTYPE html>"en">"UTF-8"> <title>Title</title> <style>Body {display:-webkit-Flex;            Display:flex; -webkit-justify-Content:center; Justify-Content:center; }    </style>"width:300px;height:100px; border:1px Solid"> ""Id="Imgnode"></div></body><script>Document.addeventlistener ('Paste', Function (Event) {Console.log (Event); varIschrome =false; if(Event. clipboarddata | |Event. Originalevent) {            //Some versions of Chrome are using Event.originalevent            varClipboarddata = (Event. clipboarddata | |Event. Originalevent.clipboarddata); if(clipboarddata.items) {//For Chrome                varItems =Clipboarddata.items, Len.=Items.length, Blob=NULL; Ischrome=true;  for(vari =0; i < Len; i++) {Console.log (items[i]); if(Items[i].type.indexof ("Image") !== -1) {                        //Getasfile () This method is only living standard Firefox IE11 does not supportBlob =Items[i].getasfile (); }                }                if(blob!==NULL){                    varBloburl=Url.createobjecturl (BLOB); //Blob Object Displaydocument.getElementById ("Imgnode"). src=Bloburl; varReader =NewFileReader (); //Base64 Code Display                   /*reader.onload = function (event) {//Event.target.result is the BASE64 encoded string for the picture                        var base64_str = Event.target.result;                    document.getElementById ("Imgnode"). Src=base64_str; } reader.readasdataurl (BLOB);*/                    //with canvas, generate Formdata                    varCanvas = document.createelement ('Canvas'); varDataurl = Canvas.todataurl ('Image/jpeg',0.5); varFD =NewFormData (document.forms[0]); Fd.append ("The_file"Blob'Image.png'); //Create a XMLHttpRequest object                    varXHR =NewXMLHttpRequest (); Xhr.open ('POST','/image' ); Xhr.onload=function () {if(Xhr.readystate = = =4 ) {                            if(Xhr.status = = = $ ) {                                vardata =Json.parse (Xhr.responsetext);                            Console.log (data); } Else{console.log (xhr.statustext);                    }                        };                    }; Xhr.onerror=function (e) {console.log (xhr.statustext);                } xhr.send (FD); }            }        }    })</script>

JS Implementation Ctrl + V paste pictures or

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.