Paste the image in the input box and upload it to the server. How can this problem be achieved using PHP? (This is not the way to upload local files, but from the clipboard) paste an image in the input box and upload it to the server. How can this problem be achieved using PHP? (This is not the way to upload local files, but from the clipboard)
Reply content:
Paste the image in the input box and upload it to the server. How can this problem be achieved using PHP? (This is not the way to upload local files, but from the clipboard)
The general principle is: Listen to the pasting time, check whether there is an image, and then trigger AJAX upload. The receiving principle of PHP is consistent with that of file upload.
Script // search for the box element to check the document. querySelector ('# box '). addEventListener ('paste ', function (e) {// determine whether the image is pasted if (e. clipboardData & e. clipboardData. items [0]. type. indexOf ('image')>-1) {var that = this, reader = new FileReader (); file = e. clipboardData. items [0]. getAsFile (); // ajax uploads an image reader. onload = function (e) {var xhr = new XMLHttpRequest (), fd = new FormData (); xhr. open ('post', '', true); xhr. onload = function () {var img = new Image (); img. src = xhr. responseText; // that. innerHTML = ''; document. getElementById ("img_puth "). value = img. src;} // this. result to get the base64 (which can be used for instant display) fd of the image. append ('file', this. result); that. innerHTML = ''; xhr. send (fd);} reader. readAsDataURL (file) ;}}, false); script