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>
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.
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.
Using type="file" and accept="image/*" (or the format you want), allow the user to chose a file with specific format.
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!
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With