Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Paste Clipboard Image to File Input

Okay, here it goes. I have seen the great ways in which one can now paste images to WhatsApp web chats. Most examples use a canvas to capture the pasted clipboard image, how does one paste it to a File Input using only Ctrl + V anywhere on the page?

Here is the code I have for the input which automatically submits as soon as someone selected a file:

      <form id="new_document_attachment" method="post">
            <div class="actions"><input type="submit" name="commit" value="Submit" /></div>
                <input type="file" id="document_attachment_doc" />
      </form>
      <script>
          document.getElementById("document_attachment_doc").onchange = function() {
          document.getElementById("new_document_attachment").submit();
        };
      </script>
like image 591
doer123456789 Avatar asked May 19 '18 16:05

doer123456789


People also ask

How do I copy and paste an image in HTML?

3. Copy and paste your image URL into an IMG tag, add a SRC to it. Identify first where you'd like to place your image within the HTML and insert the image tag, <img>. Then take your uploaded image, copy the URL and place it within your img parameters prefaced by a src.

How do you paste an image into clipboard?

If you've put an image in the clipboard by using Ctrl+c or by right-clicking and choosing Copy or Cut, then you paste that image by using Ctrl+v or Paste in whatever location you want the copy to go.

How do I import an image into an input type file?

Using type="file" and accept="image/*" (or the format you want), allow the user to chose a file with specific format.

How do you attach a file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

It's pretty straightforward. Just catch paste event on window object and put all the files you got from it to the <input> tag.

const form = document.getElementById("new_document_attachment");  const fileInput = document.getElementById("document_attachment_doc");    fileInput.addEventListener('change', () => {    form.submit();  });    window.addEventListener('paste', e => {    fileInput.files = e.clipboardData.files;  });
<form id="new_document_attachment" method="post">    <div class="actions"><input type="submit" name="commit" value="Submit" /></div>    <input type="file" id="document_attachment_doc" />  </form>
like image 197
Jake Weary Avatar answered Sep 21 '22 06:09

Jake Weary


Work well

<form action="abc.php" method="post" enctype="multipart/form-data">   <div class="actions"><input type="submit" name="commit" value="Submit" /></div>   <input type="file" name="aaa"/> </form>     <script type="text/javascript"> //e.originalEvent.clipboardData.files const form = document.getElementById("new_document_attachment"); const fileInput = document.getElementById("document_attachment_doc");  fileInput.addEventListener('change', () => {   form.submit(); });  window.addEventListener('paste', e => {   fileInput.files = e.clipboardData.files; });   </script> 

PHP Output: var_dump($_FILES);

array (size=1)   'aaa' =>      array (size=5)       'name' => string 'image.png' (length=9)       'type' => string 'image/png' (length=9)       'tmp_name' => string 'C:\wamp64\tmp\phpF6AF.tmp' (length=25)       'error' => int 0       'size' => int 9380 
like image 40
Hakan Avatar answered Sep 19 '22 06:09

Hakan