Is there a way to upload a file without using an <input type="file">
I want the user to upload their file when clicking on a photo
Based on JavaScript
Asked
Active
Viewed 77 times
-1
-
1to specify a file location or to show a file choose dialog box, you depend on the browser's inbuilt code, so the technical answer is NO, but you can hide `input` tag and trigger the click dynamically – Dickens A S Mar 29 '20 at 02:49
1 Answers
0
I believe you will need the input but you can hide it with CSS so the user is not forced to interact with it, then you provide functionality of the input through javascript click event on an image.
Basically, this is the technique, already answered here but adapted for an image:
https://stackoverflow.com/a/12436476/1161948
$("#select_logo").click(function(e){
e.preventDefault();
$("#logo").trigger('click');
});
#logo { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Click the image to upload</div>
<img src="https://placehold.it/150x50" id="select_logo"><input type="file" id="logo">
Then, depending on what type of file you are expecting uploaded, you will need to do more work to read the contents of the file. Here's an example: https://stackoverflow.com/a/44161989/1161948

ThisClark
- 14,352
- 10
- 69
- 100
-
It's different because in nature when you click change the link and this is what we've discarded , it's left to work what's inside and the DIV is normal when you press nothing so we don't need to do preventDefault i.e. you have to change the input size to cover every DIV and do opacity 0 – sifo ou Apr 01 '20 at 15:08
-
I hide the input and "click" it with a script whenever the image is clicked on. I do not cover anything with a resized input field. The div is just an instruction and has nothing to do with the behavior of the image click. – ThisClark Apr 01 '20 at 15:45
-