I’m using an Upload Script, and I’ve been working on trying to style the ‘Choose File Button’ (input file button)
which was a challenge to be styled for multiple browsers, but finally works with Google, IE and FF.
However, now that I’ve successfully styled the input file button, when a file is now chosen, the chosen file name no longer appears, prior to Submit.
I’ve attached an image of the code changes I made, plus some CSS styling, and this addition:
I think it’s easier to use JS (JavaScript, not the same as Java) to create styled upload button.
All you need to do is to create button or another element, then you need to hide file input.
Then you need to add 'click event on button to imitate the same click on input and add ‘change’ event to input which will change button’s inner text to the value of the input.
Thanks for your replies.
I know there are many upload alternatives, but because this one works successfully, except for the file name being displayed, I’d prefer to find a solution for the Form I’m using, rather than substitute another one. This Form script I’m using has other attributes that I’d like to keep, which may not be available on other alternatives. Even if I can’t find a solution to show the file name, I’m also looking for ideas that could show something (if not a file name) to show that the file selection action has taken place. Currently, nothing appears once a file is chosen. Any ideas/suggestions will be appreciated.
Can you post your code on JSFiddle or CodePen or other sandbox service?
Because it’s would be easier to understand for us if you’ll upload code and show not only the code but also how it would be looked like.
P.S.: Are you using CI’s form validation?
Thanks for your reply.
Yes it’s supposed to look like that http://jsfiddle.net/Kt4g9/ except with all the functionality of this: http://jsfiddle.net/Kt4g9/1/
And by all the functionality, I mean having the ‘no file chosen’ appear and then a file name appear.
I’m trying to simply make it look better than this: see image
But I’d like to keep the ‘no file chosen’ and subsequently the file name to appear, once a file is selected.
It seems kind of impossible without JS. File inputs very hard to style, or I would say even impossible with CSS. The only solution, I can think of, is using simple JS code.
I know it sometimes happens when you want to do something in CSS only for Graceful Degradation, but not everything can done in CSS.
Browsers purposefully make inputs near impossible to style due to it affecting user experience. Your best bet would probably be to just use a <button> and use JS to make it behave. For those without JS, you could have a default plain <input>.
Thanks for all the replies.
At this point then, maybe someone can simply help me do one of the following:
Get the value of the file selected and just add that to a div next to the styled input file button.
Capture the file name from the DOM once the file is selected and then write it to a another HTML element located at the same spot
Have a simple a message appear, once a file is selected that says “File Selected”
just so the uploader knows that an action has taken place, prior to submit.
Any help with one of these, or another suggestion, will be welcomed.
Much thanks again
I removed the fakepath description because that will scare users no end (it did me :)) and it will also make sure that the text doesn’t spill right over the page with incredibly long paths.
Of course as I said at the beginning there are many plugins around that take care of all the browsers differences for you if you want older support.
Thanks for all the replies. I added the Paul O’b code and it works successfully in Google (thanks),
But in IE the “click to select file” button doesn’t do anything. To upload a file the browse button,
apparently needs to be selected. If there is some coding to fix that, I’d be interested in heariung about it.
see attached image files
Thanks again. I look forward to any additional assistance.
Google: IE:
Thanks for your reply/help.
When I change the line to this:
<input id="input" type="file"></input>
I get this:
Notice: Undefined index: file in /home/public_html/upload_file.php on line 7
Notice: Undefined index: file in /home/public_html/upload_file.php on line 8
Those Notices disappear. However, the issue remains the same in IE, which is that the “click to select file” button doesn’t do anything(that I can see), and to upload a file the Browse button, needs to be selected.
And it still looks like this - see IE-image in previous post.
Someone suggested this:
“with JavaScript or Jquery… Create an event handler that if a file is selected $(‘.selectedfile’).show(); <span class=“selectedfile”>Your File has been Chosen for Upload!</span> then once the submit button has been hit you could also swap that out for a loader gif or something”. But I don’t know how to do that.
Any additional help will be welcomed.
Much thanks again.