Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

html - Bootstrap file select fields not updating after file selected

I am using Bootstrap v4.5.0 in a Django template (not sure if relevant). Specifically I am using Bootsrap form components (as per https://getbootstrap.com/docs/4.5/components/forms/) to display my form elements.

While I can use the form to select a file, and upload it fine, the form never updates to show that a file has been selected. This is a problem as I have had users think my web application is not accepting their files without reason.

I'm including all the JavaScript that would be needed, and I have tested in different browsers, so I am unsure what the problem is. I would prefer not to use a different version of Bootstap at this stage, and don't believe it is an issue with the version anyway.

I have attached a snippet demonstrating the issue:

<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</head>
<body>
<div class="custom-file">
    <input class="custom-file-input" id="customFile1" name="testfile" type="file" /><label class="custom-file-label" for="customFile1">Upload a file</label>
</div>
</body>
</html>
question from:https://stackoverflow.com/questions/66056205/bootstrap-file-select-fields-not-updating-after-file-selected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use javascript to show the name of the chosen file.

$('#inputFile').on('change', function() {
  //get the file name
  var fileName = $(this)[0].files[0].name;
  //replace the "Choose a file" label
  $(this).next('.file-name').html(fileName);
})
<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="inputFile" />
      <label class="file-name" for="inputFile">Click Me To Choose File </label>
    </div>

  </div>
  </div>

</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...