-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to display selected file name? #528
Comments
Thanks for the feedback. I know that the file upload control is implemented in a different way than some of the other controls. I don't know why, as it was done before I was a maintainer. I'm sure there was a good reason. I will add this to my list of issues to look at. In the meantime, you can implement your own fields to give feedback. That's what I've done. |
Thanks for the kind comment. What do you mean by your own fields? Do you have an example of what you've done please? |
I have one example I can share:
The text field named I don't have any examples where I was uploading multiple files and showing a count on the screen. Sorry. I hope this helps. |
Thank you for being so helpful! |
@iraszl Here's a naive way of showing the name of the file immediately after the file has been selected. No need for custom fields. // Workaround for displaying selected file
$(document).on('ready turbolinks:load', function() {
$('.custom-file-input').change(function(){
$('.custom-file-label').text(this.value);
});
}); This works for my file field: <%= bootstrap_form_for @listing do |form| %>
...
<%= form.file_field :image, direct_upload: true, accept: "image/png,image/gif,image/jpeg", label: "Add an image" %>
...
<% end %> |
@codeandclay Thank you so much! I'm going to test this out ASAP! |
This is the recommended solution by the bootstrap folks import bsCustomFileInput from 'bs-custom-file-input';
$(document).ready(function () {
bsCustomFileInput.init();
}) |
@codeandclay Solution works. |
@iraszl If you're using Turbolinks (by default Rails does), perhaps @hwhelchel 's solution will work if you change the second line as follows:
You'll also have to make sure you include the package. Follow the link in this comment for instructions. |
This is an elegant solution. At the moment, |
@lcreid Works great. But note that this line is not needed in the javascript if you load the library: |
* Handle both .csv & .xlsx file uploads * Fixes bs-custom-file-input UI filename issue as per: bootstrap-ruby/bootstrap_form#528 * Add validation to ensure file contains at least one facility
FWIW, while this issue is open, you can solve the issue by sprinkling a bit of script into your form:
|
If you have multiple file fields, you could do something like this so they all don't get updated to show the same value:
|
@davidray nice catch. Your solution works both when |
Here is the solution that worked for me on a Rails 6 app. A big thanks to @hwhelchel and @lcreid for their comments above. From the terminal run:
Add the following to your project's javascript/packs/application.js file: import bsCustomFileInput from 'bs-custom-file-input';
$(document).on('ready turbolinks:load', function() {
bsCustomFileInput.init();
}) Use one of these in your view: <%= f.file_field(:image) %>
<%= f.file_field(:images, multiple: true, placeholder: "Choose files") %> |
For novices like me and who work just with plain JS (in case you use turbolinks) [based on the solution by @codeandclay]: Add this function to app/assets/javascript/custom/[file_name].js: document.addEventListener('turbolinks:load', function() {
var fileInput = document.querySelector('.custom-file-input');
var fileLabel = document.querySelector('.custom-file-label');
fileInput.addEventListener("change", (e) => {fileLabel.textContent = e.target.value.replace(/^.*[\\\/]/, ''); });
}, false); And in your file_field you don't need to change a thing <%= bootstrap_form_for(@project, local: true) do |form| %>
<%= form.file_field :attachments, label_class: "text-muted", label: "Add attachment" %>
<%= form.submit %>
<% end %> After file was selected you'll see this: |
Without the bootstrap_form gem by default the rails forms show the filename or the number of files when selected, so the user knows they successfully selected a file to be uploaded in the form.
However, when I use bootstrap_form, the file name and count is gone. Is there a way to bring it back, otherwise the users think that they failed to select the file and try again, and sometimes give up thinking the file select in the form is broken.
The text was updated successfully, but these errors were encountered: