I have a thumbnail interface mockup, and I would like to add the ability to upload an image. According to my mockup, the interface looks like this:

The intent was that the user would click on the “Add Icon” button, and a file dialog would pop up, just like the one that pops up when a file input is clicked. I would like the use the W3C’s File API to handle the file upload asynchronously, and display the thumbnail in the gray area above the button once the upload is complete.
So, I’d like to upload a file without using a file input HTML element. I tried working with a hidden file input element and calling its click event handler with Javascript when the “Add Icon” button is clicked, but the call didn’t do anything. I think security might have some reason to do with this.
Has anyone had any luck using an HTML button to upload a file before?
The trick I always do is wrapping the hidden
file inputinto alabeltag and then putting my custominputelements in thelabeltag. By defenition clicking on anything inside thelabeltag should trigger theinputinside. No JavaScript needed at all!CSS:
input{visibility:hidden;}Please note that you can’t usedisplay:nonebecause then the element will not render. Still you can make the elementwidth:0; height:0;to hideinputelement completely.Fiddle
Using HTML file API is a little different. Read this article at Mozilla Developer Network to leran more.