In order to upload multiple file to the server the basic thing what we require is multiple file fields. we can do it easily in .erb file with the help of javascript.

So just follow the steps and have fun.


Step 1 :  create a simple html div for it.
                <div class="apply_div  " id="number_of_download_filed" >
       <div class="attachment_div_text" id='div_attached_1'>Add Attachment,</div>
<div class="attachment_div_field" id="div_attached_field_1">
<input id='attached_1' type='file' name='attached_1' class='event_action_attach_fields input_file_class'>
</div>
            </div>
           <div class="apply_div ">
                             <%= button_tag 'Attach another file',  :type => 'button',  :class="attach_another_file_button"  %>
            </div>
Now what we want is just on the "attach another file" button there is another file uploading button in the page. For that we need little bit javascript.

Step 2: in the .js file write the below code
             $(document).ready(function(){
              var file_count=1 
                   $('.attach_another_file_button').on("click", function() {
                                         file_count++;
               $('#number_of_download_filed').append("<div class='attachment_div_text' " +                              +"id='div_attached_"+file_count+"'>"+"Upload another file</div>" +
"<div class='attachment_div_field'  id='div_attached_field_"+file_count+"'>" +
"<input id='attached_"+file_count+"' type='file' name='attached_"+
                                                  file_count+"' class='event_action_attach_fields' />"+"</div>");
                           });
                     });
So on clicking the "attach another file" button a next file field will be added to under the same div. You can design the same thing using  css according to your wish.

Comments

Popular posts from this blog

Insert Bulk data using active record import

Swap primary keys between two records in the same table in Postgres

How to add a bootstrap table with fixed header and scrollable body