Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap 4 file input doesn't show the file name [duplicate]

I have a problem with the custom-file-input class in Bootstrap 4. after I chose which file I want to upload the filename do not show.

I use this code:

<div class="input-group mb-3">   <div class="custom-file">     <input type="file" class="custom-file-input" id="inputGroupFile02">     <label class="custom-file-label" for="inputGroupFile02">Choose file</label>   </div>   <div class="input-group-append">     <button class="btn btn-primary">Upload</button>   </div> </div> 

Any idea how I can fix it without getting too complicated?

like image 854
Terchila Marian Avatar asked Feb 04 '18 23:02

Terchila Marian


People also ask

How do I customize bootstrap input?

To create a custom file upload, wrap a container element with a class of . custom-file around the input with type="file". Then add the . custom-file-input to it.


2 Answers

You need to use javascript to show the name of the choosed file, as written in the documentation: https://getbootstrap.com/docs/4.5/components/forms/#file-browser

Here you can find the solution: Bootstrap 4 File Input

That's the code for your example:

<html lang="en">     <head>         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">         <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>         <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>         <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>     </head>     <body>         <div class="input-group mb-3">             <div class="custom-file">                 <input type="file" class="custom-file-input" id="inputGroupFile02"/>                 <label class="custom-file-label" for="inputGroupFile02">Choose file</label>             </div>             <div class="input-group-append">                 <button class="btn btn-primary">Upload</button>             </div>         </div>         <script>             $('#inputGroupFile02').on('change',function(){                 //get the file name                 var fileName = $(this).val();                 //replace the "Choose a file" label                 $(this).next('.custom-file-label').html(fileName);             })         </script>     </body> </html> 
like image 68
debe Avatar answered Sep 22 '22 09:09

debe


I have used following for the same:

<script type="application/javascript">     $('input[type="file"]').change(function(e){         var fileName = e.target.files[0].name;         $('.custom-file-label').html(fileName);     }); </script> 
like image 44
Anuja Patil Avatar answered Sep 22 '22 09:09

Anuja Patil