making a input type "file" hidden with button
Asked Answered
G

10

37

I was wondering if this is doable? Google comes up with nothing.

Basically i want to my button to simulate when selected, allowing users to upload files. I've read its possible with labels, but i need to do with a button to do UX/UI

<button style="position:fixed;bottom:10px;right:70px" type="button" class="btn btn-default">
    <input type="file" id="files" visbility="hidden" />
    <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
</button>

Currently my button looks like this: enter image description here to this:

enter image description here

Grimy answered 25/3, 2015 at 8:51 Comment(1)
You can use the hidden attribute instead visbility="hidden" like: <input type="file" id="files" hidden /> (w3schools.com/tags/att_global_hidden.asp)Newt
T
77

You can actually do it with label, you just have to hide the input.

<!-- head -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<!-- body -->
<label for="upload">
      <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
      <input type="file" id="upload" style="display:none">
</label>
Taub answered 25/3, 2015 at 8:57 Comment(3)
Note that it doesn't work on old Android 4.2 browser.Kermit
"for" in the label Tag and "id" in the input Tag have to match. The rest is just for styling.Game
Note that Android 4.2 and less represents 0.1% of usersFortissimo
C
17

try an opacity of 0

opacity:0;

https://jsfiddle.net/m4e0y2o3/12/

plus fallbacks if you what to support older browsers

.hiddenFileInput > input{
  height: 100%;
  width: 100;
  opacity: 0;
  cursor: pointer;
}
.hiddenFileInput{
  border: 1px solid #ccc;
  width: 100px;
  height: 100px;
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  
  /*for the background, optional*/
  background: center center no-repeat;
  background-size: 75% 75%;
  background-image:  url(data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBoZWlnaHQ9IjUxMnB4IiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9Ii01MyAxIDUxMSA1MTEuOTk5MDYiIHdpZHRoPSI1MTJweCI+CjxnIGlkPSJzdX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ZnPgo=)
}
<span class="hiddenFileInput">
  <input type="file" name="theFile"/>
</span>
Conscientious answered 25/3, 2015 at 9:1 Comment(4)
get a 404 for that fiddle.Michalmichalak
On hover cursor isn't become as pointer on top ( I guess need to add some offsetMord
@victor-radu, can you, please, explain the reason for you choosing opacity: 0 instead of display: none. A little bit of reasoning (explaining when opacity: 0 will be better) will be helpful for future reference. Thank you!Adal
hello, @BorisD.Teoharov display none does not actually render the element in the dom, making interactions with it hardly possible. Opacity 0 renders the element but makes it completely transparent meaning that it will still catch the click interaction as you are clicking on it even if you do not see it.Conscientious
W
5

If you're using Angular I suppose this pretty solution

  <button class="" (click)="uploader.click()">
     <input hidden type="file" #uploader />
  <button>

you can easly make the analogy with other techno

Wicketkeeper answered 14/6, 2021 at 20:36 Comment(2)
This is a perfect solution when using Angular.Lagunas
How about the angular case when button is in the button row above the program and the place to display input image are in separated group i.e.:display section is in sketch.component.html / sketch.component.ts / sketch.component.spec.ts / topnav.component.scss while the button component is in topnav.component.html / topnav.component.ts / topnav.component.spec.ts / topnav.component.scss? Could you show me the example how to implement such a thing?Kathe
P
3

As all the other answers mention various ways of hiding the <input> element, consider using opacity: 0 as well, as quoted from the MDN docs:

Note: opacity is used to hide the file input instead of visibility: hidden or display: none, because assistive technology interprets the latter two styles to mean the file input isn't interactive.

You can pretty much trigger the file input with any other event (eg: button click, hover, etc.), you can call click() on the input element (getting it with getElementById(), querySelector(), etc.)

const inputElement = document.getElementById('file-input-element')

// on some other event
inputElement.click() // will let user select files
Plotter answered 24/9, 2021 at 7:27 Comment(0)
L
1

Something like this will do:

<button id="fileup" style="position:fixed;bottom:10px;right:70px" type="button" class="btn btn-default" >
<input type="file" id="files" visbility="hidden"/>
<span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
</button>
<script>
$('#fileup').click(function() {
   $('#files').click();
});
</script>
Lamoureux answered 25/3, 2015 at 8:57 Comment(2)
I think you better change the visibility="hidden" to style="display: none"Lamoureux
To know more about difference, visibility hidden vs display none.Sagerman
J
1

A nice way of doing this is making the input field to be

  • positioned absolute inside parent element
  • having zero opacity
  • filling the whole parent element

Parent element in this example is a button.

<button 
  style="padding: 0.625rem; border-radius: 9999px; background-color: #1F87CC; position: relative;"
>
  <input
    type="file"
    style="position: absolute; bottom: 0px; top: -1px; left: 0px; right: 0px; opacity: 0;"
  >
  <svg viewBox="0 0 20 20" fill="#FFFFFF" aria-hidden="true" style="width: 1rem; height: 1rem;"><path fill-rule="evenodd" d="M15.621 4.379a3 3 0 00-4.242 0l-7 7a3 3 0 004.241 4.243h.001l.497-.5a.75.75 0 011.064 1.057l-.498.501-.002.002a4.5 4.5 0 01-6.364-6.364l7-7a4.5 4.5 0 016.368 6.36l-3.455 3.553A2.625 2.625 0 119.52 9.52l3.45-3.451a.75.75 0 111.061 1.06l-3.45 3.451a1.125 1.125 0 001.587 1.595l3.454-3.553a3 3 0 000-4.242z" clip-rule="evenodd"></path></svg>
</button>

Which will result in looking like this. The rounded button is working as an input field of type "file".

result

Jaquelinejaquelyn answered 27/9, 2023 at 19:19 Comment(0)
D
0

For me this works:

<label htmlFor="inputGroupFile01">
     <span className="fa fa-file-image fa-lg"/>
</label>
Desertion answered 24/10, 2021 at 11:25 Comment(0)
S
0

We can use style="opacity: 0" in the input:

<input type="file" id="files"  style="opacity: 0"/>

Opacity versus Visibility:
opacity is used to hide the file input instead of visibility: hidden or display: none, because assistive technology interprets the latter two styles to mean the file input isn't interactive. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file

Your Button (with only folder icon):

<button style="position:fixed;bottom:10px;right:70px" type="button" class="btn btn-default">
    <input type="file" id="files"  style="opacity: 0"/>
    <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
    button
</button>

Bona Fortuna!

Sletten answered 1/6, 2023 at 13:51 Comment(0)
W
-1

Simple Method with beautiful input file [type="file"].

Output Image URL - https://i.sstatic.net/4Jl0H.jpg

input[type="file"] {
	display: inline-block;
	opacity: 0;
	position: absolute;
	margin-left: 20px;
	margin-right: 20px;
	padding-top: 30px;
	padding-bottom: 67px;
	width: 85%;
	z-index: 99;
	margin-top: 10px;
	cursor:pointer;
}
.custom-file-upload {
	position:relative;
    display: inline-block;
    cursor: pointer;
	padding-top:40px;
	padding-bottom:40px;
	background:url(http://www.a-yachtcharter.com/search-fleet/webaccess/assets/img/uploadIcon.png) #fff center center no-repeat;
	width:91%;
	border:1px dashed #ff5b57 !important;
	margin-left:20px;
	margin-right:20px;
	margin-top:10px;
	text-align:center;
}
<input type="file" multiple class="form-control">
<label for="file-upload" class="custom-file-upload"></label>
Walkup answered 19/7, 2017 at 8:41 Comment(0)
K
-1

You can use the following to make an input type "file" hidden with a button:

<div>
     <input type="file" id="files" style="display:none"/>
     <button onclick="click_the_button(files);">Click me</button>
</div>enter code here

<script>
    function click_the_button(btn){
        btn.click();
    }
</script>
Kinescope answered 22/9, 2018 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.