15

Using simple_form_for, Bootstrap and Rails 3. In a form:

<%= f.input :upload, label: 'PDF file:' , input_html: {accept: ('application/pdf') } %>

I don't know how I'd style this so that the "choose file" button can have a different class ('btn btn-primary').

Additionally, when using with Bootstrap at least, its severely misaligned by default. See attached image.

Finally, how do I redefine the text from "No file chosen" to "Chose file" when there isn't one added yet, and show the file name when there is one.

enter image description here

tibbon
  • 1,018
  • 2
  • 16
  • 30

9 Answers9

24

This is how I do it:

  1. In the view add your form file field and hide it
  2. Add a styled additional field just to display the file name
  3. Add a button to trigger the file browse dialog

    <div class="control-group">
      <div class="attach-set">
        <%= f.input :real_file, input_html: { hidden: true }, label: 'Upload Attachment' %>
        <div class="input-append">
          <input id="file-display" class="input-large uneditable-input" type="text">
          <a id="upload-btn" class="btn"><i class="icon-upload-alt"></i> Browse</a>
        </div>
      </div> <!-- /attach-set -->
    </div> <!-- /control-group -->
    
  4. In your JS (Coffee w/ jQuery shown), pass the click from the display button onto the real file input and when they select a file drop the file name in the display text field (I drop the path so that I don't see C:\FakePath....)

    $(document).ready ->
    
      # ------------------------------------------------------
      # pretty-fy the upload field
      # ------------------------------------------------------
      $realInputField = $('#real_file')
    
      # drop just the filename in the display field
      $realInputField.change ->
        $('#file-display').val $(@).val().replace(/^.*[\\\/]/, '')
    
      # trigger the real input field click to bring up the file selection dialog
      $('#upload-btn').click ->
        $realInputField.click()
    
BSB
  • 1,516
  • 13
  • 14
  • Great answer. I have only one question: what does the 'uneditable-input' class does? – Ernesto Nov 18 '14 at 17:34
  • uneditable-input it's a bootstrap class that just disables the input field so that the only way to change the contents is via the file picker – BSB Nov 19 '14 at 18:27
  • 1
    My file input does not seem to get hidden, does anyone have any idea why? – Wraithseeker Dec 15 '15 at 16:24
  • @Wraithseeker, The hidden: true is overwritten by display: block of input[type="file"] when I did the inspect input[type="file"] { 1. display: block; } [hidden], template { 2. display: none; } You can add a class inside input_html: {class: 'hidden-input'} and a more specific rule in you scss: products.scss // Product new/edit (ie. _form) input[type="file"].hidden-input { display: none; } – frank Jun 06 '16 at 18:08
  • I had to do this (HAML): = f.input :pic, as: :hidden, input_html: { hidden: true, type: "file", class: "file required" } to hide the original file upload box. Also, to get it readonly the "uneditable-field" class didn't work for me so I added readonly: true. Works great! – Guy Aug 06 '16 at 16:49
13

This worked great for me and only requires HTML

<label class="btn btn-primary">
  Add a file!
  <span style="display:none;">
    <%= f.file_field :image, required: true, multiple: true, name: 'picture' %>
  </span>
</label>
Yoko
  • 793
  • 10
  • 19
6

I ran across and am using Jasny's extension to Bootstrap 3. It seems to work well so far.

kross
  • 3,627
  • 2
  • 32
  • 60
4

No JS required, just plain css

scss

.fileinput-button {
  position: relative;
  overflow: hidden;
  float: left;
  margin-right: 4px;
  width: 110px;
  height: 32px;
  input{
    opacity: 0;
    filter: alpha(opacity=0);
    transform: translate(-300px, 0) scale(4);
    direction: ltr;
    cursor: pointer;
  } 
}

html / slim

span class="btn btn-success fileinput-button"
  i.fa.fa-pencil
  span
   |  Select File
  = f.file_field :cover_ar

I recommend using compass for cross browser compatibility

El'Magnifico
  • 798
  • 6
  • 15
Max
  • 281
  • 3
  • 11
  • You have two small typos in your CSS (one missing semicolon, one missing }), and for Safari support, it is helpful to add `-webkit-transform: translate(-300px, 0)scale(4);`. Other than that, great solution, thanks a lot! – Thomas Walther Dec 11 '14 at 21:16
2

As @rafaelfranca said you can't style file input but you can add your own button which will be clicking your hidden original button. See example here http://jsfiddle.net/rUdf2/6/

Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
2

Every Browser has a different type of file input field button and this makes it a pain. You can play a little with css. This has given me a basic styling with JS without the annoying "No file chosen" text in chrome and Safary:

$(document).ready(function() {
  $(".your_button").css("width", "80px");
});

Otherwise the best solution is to hide it and show a fake one that intercepts the click:

http://duckranger.com/2012/06/pretty-file-input-field-in-bootstrap/

With respect to the question of how to show that a file has been uploaded, a basic solution with jquery file upload is to detect the upload complete event and replace some of your text with a success message (The exact file name I believe it is not possible to obtain with modern browsers):

$(".your_button").fileupload({
    dataType: "json",
    done: function(e, data) {
        $(".place_for_your_text").text("File uploaded.");
    }
});

In summary, a basic solution is to use javascript in your assets to:

  1. Hide the annoying "No file chosen text" with css.
  2. Place your "Chose file" text next to the button and give it a class you can reference.
  3. Replace the text with "File uploaded"
joscas
  • 7,474
  • 5
  • 39
  • 59
0

No fancy shiz required:

HTML:

<form method="post" action="/api/admin/image" enctype="multipart/form-data">
    <input type="hidden" name="url" value="<%= boxes[i].url %>" />
    <input class="image-file-chosen" type="text" />
    <br />
    <input class="btn image-file-button" value="Choose Image" />
    <input class="image-file hide" type="file" name="image"/> <!-- Hidden -->
    <br />
    <br />
    <input class="btn" type="submit" name="image" value="Upload" />
    <br />
</form>

JS:

$('.image-file-button').each(function() {
      $(this).off('click').on('click', function() {
           $(this).siblings('.image-file').trigger('click');
      });
});
$('.image-file').each(function() {
      $(this).change(function () {
           $(this).siblings('.image-file-chosen').val(this.files[0].name);
      });
});

CAUTION: The three form elements in question MUST be siblings of each other (.image-file-chosen, .image-file-button, .image-file)

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
mattdlockyer
  • 6,984
  • 4
  • 40
  • 44
0

If you are using Bootstrap, Simple Forms, Jasny, and ReFile, this post may be of interest to you refile simple_form undefined method attachment_field

Community
  • 1
  • 1
Chris Hough
  • 3,389
  • 3
  • 41
  • 80
0
<label class="btn btn-primary"
<%= f.input :upload, label: 'PDF file:',
class:"hidden", input_html: {accept: ('application/pdf') } %>
</label>

Works for me

Using class="hidden"

PS: I'm using Tailwind CSS