web-dev-qa-db-fra.com

faire un type d'entrée "fichier" caché avec le bouton

Je me demandais si cela est faisable? Google n'apporte rien.

Fondamentalement, je veux simuler mon bouton lorsque sélectionné, permettant aux utilisateurs de télécharger des fichiers. J'ai lu sa possible avec des étiquettes, mais je dois faire avec un bouton pour faire 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>

Actuellement, mon bouton ressemble à ceci: enter image description herepour ça:

enter image description here

14

Vous pouvez réellement le faire avec label, il vous suffit de cacher la 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>

32
qtgye

essayez une opacité de 0

opacity:0;

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

plus des solutions de rechange si vous voulez quoi supporter les navigateurs plus anciens

.hiddenFileInput > input{
  height: 100%;
  width: 100;
  opacity: 0;
  cursor: pointer;
}
.hiddenFileInput{
  border: 1px solid #ccc;
  width: 100px;
  height: 100px;
  display: inline-block;
  overflow: hidden;
  
  /*for the background, optional*/
  background: center center no-repeat;
  background-size: 75% 75%;
  background-image:  url(data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBoZWlnaHQ9IjUxMnB4IiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9Ii01MyAxIDUxMSA1MTEuOTk5MDYiIHdpZHRoPSI1MTJweCI+CjxnIGlkPSJzdXJmYWNlMSI+CjxwYXRoIGQ9Ik0gMjc2LjQxMDE1NiAzLjk1NzAzMSBDIDI3NC4wNjI1IDEuNDg0Mzc1IDI3MC44NDM3NSAwIDI2Ny41MDc4MTIgMCB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4IDIxNS42ODc1IDM1NS45MjU3ODEgMjE1LjY4NzUgMzQ5LjEyNSBMIDIxNS42ODc1IDIxNC4wNzQyMTkgTCAyNjYuNjQwNjI1IDI2OC44NjMyODEgQyAyNjkuMTEzMjgxIDI3MS40NTcwMzEgMjcyLjMzMjAzMSAyNzIuODIwMzEyIDI3NS42Njc5NjkgMjcyLjgyMDMxMiBDIDI3OC42MzY3MTkgMjcyLjgyMDMxMiAyODEuNzMwNDY5IDI3MS43MDcwMzEgMjg0LjA3ODEyNSAyNjkuNDgwNDY5IEMgMjg5LjAyNzM0NCAyNjQuNzgxMjUgMjg5LjM5ODQzOCAyNTYuOTg4MjgxIDI4NC42OTkyMTkgMjUyLjA0Mjk2OSBMIDIxMi4yMjY1NjIgMTc0LjI1MzkwNiBDIDIwOS44NzUgMTcxLjc4MTI1IDIwNi42NjAxNTYgMTcwLjI5Njg3NSAyMDMuMTk5MjE5IDE3MC4yOTY4NzUgQyAxOTkuNzM0Mzc1IDE3MC4yOTY4NzUgMTk2LjUxOTUzMSAxNzEuNzgxMjUgMTk0LjE3MTg3NSAxNzQuMjUzOTA2IEwgMTIxLjY5OTIxOSAyNTIuMDQyOTY5IEMgMTE3IDI1Ni45ODgyODEgMTE3LjM3MTA5NCAyNjQuOTAyMzQ0IDEyMi4zMTY0MDYgMjY5LjQ4MDQ2OSBDIDEyNy41MTE3MTkgMjc0LjE3OTY4OCAxMzUuMzAwNzgxIDI3My44MDg1OTQgMTQwIDI2OC44NjMyODEgWiBNIDE0MCAyNjguODYzMjgxICIgc3R5bGU9IiBmaWxsLXJ1bGU6bm9uemVybztmaWxsLW9wYWNpdHk6MTsiIHN0cm9rZT0iIzAwMDAwMCIgZmlsbD0iIzAwMDAwMCIvPgo8L2c+Cjwvc3ZnPgo=)
}
<span class="hiddenFileInput">
  <input type="file" name="theFile"/>
</span>

3
Victor Radu

Vous pouvez utiliser ce qui suit pour faire un type de saisie "fichier" caché avec un bouton:

<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>
1
KNV

Méthode simple avec beau fichier d'entrée [type = "fichier"].

URL de l'image de sortie - https://i.stack.imgur.com/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>

0
Prafulla

Quelque chose comme ça va faire:

<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>
0
Eduardo Ramos