Adding images to many rich text editors or edit boxes is done by clicking the button-select image-Upload form, which requires the user to save the image first.
In some cases, pasting a picture directly after implementing or copying the picture makes the operation easier.
For example, paste a picture in the textarea, listen to the paste action to get the picture data in the pasteboard, and upload the picture to the server, get the URL of the picture to display in the specified place, usually insert the picture in the place where the cursor is located in textarea.
The implementation process requires only a JS file
varinput = document.getElementsByTagName ("textarea") [0];input.addeventlistener (' Paste ', Pastehandler);/*Handle Paste Event*/functionPastehandler (e) {//e:clipboardevent //Check if Event.clipboarddata is supported (Chrome) if(e.clipboarddata) {varItems =E.clipboarddata.items; if(items) { for(vari=0;i<items.length;i++){ if(Items[i].kind = = ' file ' | | items[i].type.indexof (' image ') >-1){ //need to represent the image as a file varBlob =Items[i].getasfile (); varReader =NewFileReader (); //need base64 to upload to serverReader.readasdataurl (BLOB); Reader.onload=function(e) {//progresseventUploadFile (E.target.result);//or Reader.result } } } } }};varUploadFile =function(SRC) {varFileBase64 = Src.split (",") [1]; vardata =[{base64:filebase64}]; $.ajax ({type:"POST", URL:"/feedback/attach/upload", Data:JSON.stringify ({data:data}), DataType:"JSON", ContentType:"Application/json", Success:function(data) {if(Data.code && data.msg.attachments &&data.msg.attachments.length) {varAttachs =data.msg.attachments; varLiNode = document.createelement ("li"); varImagenode = document.createelement (' img '); Imagenode.setattribute (' Src ', attachs[0].thumburl); $ (LiNode). attr ("Data-fileid", attachs[0].fileid); $ (LiNode). attr ("Data-url", attachs[0].url); Linode.appendchild (Imagenode); document.getElementById (' Images-paste '). appendchild (LiNode); }}, Error:function(Err) {Console.log (err); } });};
1. document.getElementsByTagName ("textarea"), getElementsByTagName out is an array, on the current page only one
TextArea document.getElementsByTagName ("textarea") [0] is obtained, but the Document.getelementsbyid () method is recommended.
2. Through the DOM node AddEventListener method to monitor the paste action, and make the corresponding JS processing
3. E is an event object clipboardevent, which obtains data from the Clipboard through E.clipboarddata and can be clipboard.items[i].type.indexof (' image ') To determine if the data is a picture
4. Get the picture file and get the binary data of the picture through the Getasfile () method
5. Read the file and use FileReader to read the local file
by Reader.readasdataurl (ImageFile), the image file is converted to Dataurl, which is a URL in the form of data:image/png;base64,xxx, which is placed in the src of the image element to display the local file. (If you can get a local file)
6. When the read operation succeeds, call Reader.onload, and the result property will contain a data: URL-formatted string to represent the contents of the file being read.
E object is Progressevent, This.result (This:reader) is E.target.result, that is, the base64 of the picture
7. Upload the image to the server using Base64
Get base64 encoding in Dataurl, upload to server
var fileBase64 = Src.split (",") [1]
8. After returning the picture URL from the server, put the picture in the Div with ID images-paste, and if you want to place it at the cursor, you need to write the JS processing separately.
Note: When you get a clipboard file, only chrome can support it, and Chrome gets the files in the Clipboard via the Clipboarddata object
The Clipboarddata.items property of the Chrome browser is used in conjunction with the Getasfile method to obtain binary data for the files in the Clipboard
|
Firefox 14.0.1 |
Chrome 22.0 |
Safari 6.0 |
IE 9.0 |
Opera 12.01 |
| Paste Event |
Yes |
Yes |
Yes |
Yes |
No |
| Paste event on non-editable element |
No |
Yes |
No |
No |
No |
| Clipboarddata |
No |
Yes |
Yes |
Yes |
No |
| Clipboarddata.types |
No |
Yes |
Yes |
No |
No |
| Clipboarddata.items |
No |
Yes |
No |
No |
No |
| Clipboarddata.getdata |
No |
Yes |
Yes |
Yes |
No |
| Clipboarddata.setdata |
No |
Yes |
Yes |
Yes |
No |
| MIME Types |
No |
Yes |
Yes |
No |
No |
| Custom Types |
No |
Yes |
Yes |
Yes |
No |
| Event.clipboarddata |
No |
Yes |
Yes |
No |
No |
| Window.clipboarddata |
No |
No |
No |
Yes |
No |
See:
1. MDN WEB API Https://developer.mozilla.org/zh-CN/docs/Web/API
2. Pasting image from Clipboarddata object http://codebits.glennjones.net/copypaste/pasteimagedata.htm< /c6>
3. Clipboard API and Events https://w3c.github.io/clipboard-apis/
Enables copying or pasting pictures directly