2

Not sure how to style or change the text of the "Choose File" button inside of my file upload input field.

http://codepen.io/leongaban/pen/wrCLu

<input id="choose_file" type="file" name="datafile" size="40"> input { padding: 10px 15px; border: 0; background: orange; } 

enter image description here

^ Here the background gets styled instead of the button.

6
  • 1
    Well this is not quite possible, you have to make it invisible and implement additional controls, which are allowed to be styled. Then, using JS to bind them to the file upload functionality. Commented Mar 3, 2014 at 21:34
  • 1
    Now you can just hide this and draw whatever you like then trigger the file chooser using javascript.. if you want I can make you a quick example.. Commented Mar 3, 2014 at 21:34
  • stackoverflow.com/questions/572768/… Commented Mar 3, 2014 at 21:35
  • Oh ok so basically just a positioned div with a styled button on top of this? Commented Mar 3, 2014 at 21:39
  • 1
    May be this geniuscarrier.com/… Commented Mar 3, 2014 at 21:51

4 Answers 4

1

As I told you in my comment you can simply create whatever layout and visuals you like to a button and create a file button then simply hide that file button and bind the event on the styled button to trigger the file button.

I've made this example for that purpose: Codepen with custom file button

Sign up to request clarification or add additional context in comments.

2 Comments

Ah thanks! I hope in HTML6 this issue gets resolved :)
You can use the file api in HTML5 to get full details for the files being uploaded.. anytime you need such details please write a comment on this codepen and I will enhance this example .. :-)
1

There are no native options for styling an input[type="file"] element. However, this article describes a cool (but hacky) trick you can use to accomplish this. Basically:

  1. Create a button and style the layout as you would like it to appear.
  2. Position your <input type="file" /> absolutely over the top of your new button element.
  3. Add a z-index to the element to make it one level above the styled button.
  4. Set the input to have an opacity: 0;
  5. Wire up the proper events described in the article to make the input function accordingly.

1 Comment

You can trigger it with JS without having to position it and use opacity.
1

CSS only solution

You can use the file-selector-button CSS pseudo-element

::-webkit-file-upload-button{ .. } 

more information

Comments

0

Here is my straight-forward HTML 5 solution shown using an MVC Razor Form, but you could use a plain html form just as well. This solves the problem with the Input type=file not rendering the same in all browsers. You could style the browseBtn however you like by setting a background image for it. I tested this in IE 11, Firefox, and Chrome. IMO, the look of the default Chrome native control (shown in the question) is unacceptable.

Index.cshtml

<h2>Index</h2> @using (Html.BeginForm("postFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <div style="display:inline-block; margin-bottom:10px"> <input type="text" name="uploadControl" id="uploadControl" style="width: 400px; height: 1.1em;" readonly="true" > <button type="button" id="browseBtn" >Browse...</button> </div> <input type="file" name="upfile" id="upfile" style="display:none;" > <button type="submit" id="uploadbtn" style="display:block">Click to upload</button> <br><br> @ViewBag.Message } <script src="~/Scripts/jquery-1.8.2.js"></script> <script src="~/Scripts/UploadFile.js"></script> 

UploadFile.js

$('#browseBtn').click(function () { $('#upfile').first().trigger("click"); //cause the browse menu to pop up }); $('#upfile').first().change(function (event) { event.preventDefault(); var fileName = $('#upfile').val(); if (fileName && fileName.length > 0) { $('#uploadControl').val(fileName); } }); 

HomeController.cs

 public ActionResult postFile(HttpPostedFileBase upfile) { if (upfile != null && upfile.ContentLength > 0) { try { string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(upfile.FileName)); //upfile.SaveAs(path); ViewBag.Message = Path.GetFileName(upfile.FileName) + " uploaded successfully"; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } else { ViewBag.Message = "You have not specified a upfile."; } return View("Index"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.