0

I'm want to create form with icon button. when user click on the icon browser dialog will open and allow him to choose file. after he finish i would him to have the option to click again on the icon and choose another file (limited to X files).

how can i do that?

this is what i have now: (hide = display:none )

<form class="form-horizontal" action='#' method="post"  enctype="multipart/form-data">

    <img class="uploadFileImg" alt="" src="images/photoIconOn.png"> <br /><br/>
    <span class="fileNameBox"></span>
    <input type='file' name='file[]' class='file-field hide' maxlength='2' accept="image/jpg,image/png,image/jpeg,image/gif" />

</form>

js:

$(".uploadFileImg").on('click',function(){
   $(".file-field").trigger('click'); 

    var new_field = $("<input type='file[]' class='form-control file-field hide'>");
    $(this).closest('form').append(new_field);     

}); 

check file validation:

$(function() {
    $('input.file-field').on('change', function(e) {
        var filename = $(this).val();
        if (!(/\.(gif|jpg|jpeg|tiff|png)$/i).test(filename)) {
            this.value = '';
            $(this).closest('form').find('.fileNameBox').html("<span class='valid_msg'>Only images allowed</span>");
        }
        else
            $(this).closest('form').find('.fileNameBox').html("Image: " + filename + " <button class='clear_file btn btn-default btn-xs'>remove</button>");
  });
});

Remove files:

$(function() {
    $(document).on('click','.clear_file',function() {
        $(this).closest('form').find('input.file-field').val("")
        $(this).closest('form').find('.fileNameBox').html("");
  });
});
Roi
  • 181
  • 1
  • 1
  • 8
  • possible duplicate of [How to limit maximum items on a multiple input ()](http://stackoverflow.com/questions/10105411/how-to-limit-maximum-items-on-a-multiple-input-input-type-file-multiple) – Wesley Smith Sep 29 '15 at 12:52
  • @DelightedD0D - hi. not good. i checked your link - after choosing the first file i can't choose another one while the first one still appear... – Roi Sep 29 '15 at 12:59
  • If you want that kind of functionality, id use a service like [filepicker](https://www.filepicker.com/tour/) – Wesley Smith Sep 29 '15 at 13:03

0 Answers0