Updated for the Year 2021
New Features in Bootstrap 5
The custom file input feature has been removed in Bootstrap 5. To modify the text "Choose file..." now requires the use of JavaScript or some CSS tricks like this example.
For Bootstrap 4.4 Users
To display the selected filename without custom file input, you can utilize plain JavaScript. Here's a sample code snippet assuming there is a standard custom-file-input with a label as the next sibling element to the input...
document.querySelector('.custom-file-input').addEventListener('change',function(e){
var fileName = document.getElementById("myInput").files[0].name;
var nextSibling = e.target.nextElementSibling
nextSibling.innerText = fileName
})
Code Example - Displaying Selected File Name
Introducing Bootstrap 4.1+
In Bootstrap 4.1, the placeholder text "Choose file..." is now defined inside the custom-file-label
:
<div class="custom-file" id="customFile" lang="es">
<input type="file" class="custom-file-input" id="exampleInputFile" aria-describedby="fileHelp">
<label class="custom-file-label" for="exampleInputFile">
Select file...
</label>
</div>
To change the text on the browse button, some additional CSS or SASS modifications are required. Also, notice how language translations work using the lang=""
attribute.
.custom-file-input ~ .custom-file-label::after {
content: "Button Text";
}
CSS Sample - Change Button Text
SASS Sample - Modify Button Appearance
Another Option for Bootstrap 4.1 Users
Alternatively, you can integrate this custom file input plugin
Custom File Input Plugin - Demo
Details About Bootstrap 4 Alpha 6 (Original Answer)
There seem to be 2 distinct issues at play here..
<label class="custom-file" id="customFile">
<input type="file" class="custom-file-input">
<span class="custom-file-control form-control-file"></span>
</label>
Issue 1 - Tweaking Initial Placeholder and Button Text
In Bootstrap 4, the initial placeholder value can be adjusted via the custom-file-control
using a CSS pseudo ::after
element based on the website language. The initial file button appearance (though not functional) can be modified using a CSS pseudo ::before
element. These settings can be overwritten with CSS customization..
#customFile .custom-file-control:lang(en)::after {
content: "Select file...";
}
#customFile .custom-file-control:lang(en)::before {
content: "Click me";
}
Issue 2 - Retrieving Selected Filename and Updating Input Display
Once a file is chosen, its name can be extracted using JavaScript/jQuery.
$('.custom-file-input').on('change',function(){
var fileName = $(this).val();
})
Since the placeholder text is managed through a pseudo element, manipulating it with Js/jQuery isn't straightforward. However, you can have an additional CSS class that conceals the pseudo content upon selecting a file...
.custom-file-control.selected:lang(en)::after {
content: "" !important;
}
Toggle the .selected
class on the .custom-file-control
utilizing jQuery once a file is picked. This action hides the initial placeholder value and displays the filename in the .form-control-file
span...
$('.custom-file-input').on('change',function(){
var fileName = $(this).val();
$(this).next('.form-control-file').addClass("selected").html(fileName);
})
Further processes related to file uploading or re-selection can then be handled accordingly.
Live Demonstration on Codeply (alpha 6)