You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
The text field named uri (never mind why I called it that) actually is the file name of the file that was uploaded. If you were uploading the document with an AJAX request you'd have to add some JavaScript to the solution. In my case, it was simply doing a round trip to the server, so when the page re-rendered, the file name was shown.
I don't have any examples where I was uploading multiple files and showing a count on the screen. Sorry.
This is an elegant solution. At the moment, bootstrap_form doesn't ship any JavaScript. Do you think it's good enough to document this solution in the README, rather than change the code?
@lcreid Works great. But note that this line is not needed in the javascript if you load the library: import bsCustomFileInput from 'bs-custom-file-input';
Can you confirm if I'm correct or not?
Activity
lcreid commentedon Mar 27, 2019
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.
iraszl commentedon Mar 28, 2019
Thanks for the kind comment. What do you mean by your own fields? Do you have an example of what you've done please?
lcreid commentedon Mar 28, 2019
I have one example I can share:
The text field named
uri(never mind why I called it that) actually is the file name of the file that was uploaded. If you were uploading the document with an AJAX request you'd have to add some JavaScript to the solution. In my case, it was simply doing a round trip to the server, so when the page re-rendered, the file name was shown.I don't have any examples where I was uploading multiple files and showing a count on the screen. Sorry.
I hope this helps.
iraszl commentedon Mar 29, 2019
Thank you for being so helpful!
codeandclay commentedon Apr 15, 2019
@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.
This works for my file field:
Before file has been selected:

After file has been selected:

iraszl commentedon Apr 15, 2019
@codeandclay Thank you so much! I'm going to test this out ASAP!
hwhelchel commentedon Apr 20, 2019
This is the recommended solution by the bootstrap folks
hwhelchel commentedon Apr 20, 2019
https://www.npmjs.com/package/bs-custom-file-input
iraszl commentedon Apr 20, 2019
@codeandclay Solution works.
@hwhelchel Doesn't seem to work. Do I need to add some class to the form to make it work?
lcreid commentedon Apr 20, 2019
@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.
lcreid commentedon Apr 20, 2019
This is an elegant solution. At the moment,
bootstrap_formdoesn't ship any JavaScript. Do you think it's good enough to document this solution in the README, rather than change the code?iraszl commentedon Apr 21, 2019
@lcreid Works great. But note that this line is not needed in the javascript if you load the library:
import bsCustomFileInput from 'bs-custom-file-input';Can you confirm if I'm correct or not?
WIP: Fixes bs-custom-file-input UI filename issue as per:
WIP: Fixes bs-custom-file-input UI filename issue as per:
WIP: Fixes bs-custom-file-input UI filename issue as per:
WIP: Fixes bs-custom-file-input UI filename issue as per:
Bulk upload facilities V1
Halloran commentedon Sep 21, 2019
FWIW, while this issue is open, you can solve the issue by sprinkling a bit of script into your form:
davidray commentedon Sep 23, 2019
If you have multiple file fields, you could do something like this so they all don't get updated to show the same value:
Halloran commentedon Sep 23, 2019
@davidray nice catch. Your solution works both when
multiple: trueandmultiple: falseThanksbarriault commentedon Jan 22, 2020
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:
Use one of these in your view:
anastaszi commentedon Jul 8, 2020
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:
And in your file_field you don't need to change a thing
After file was selected you'll see this: