1

I have customized my form upload button and hide the input field via css display:none but when I select an image the input is not showing the preview text!

I want to display the selected image preview text so that user know he has selected an image!

HTML:

<div class="form-group">
    <label for="before_image" class="custom-file-upload"><i class="fa fa-cloud-upload"></i> Before Image*</label>
    <input type="file" id="before_image" name="before_image" class="form-group" placeholder="Before Image" data-bind="value: before_image">                        
    <span>No file chosen</span>
</div>

CSS:

.custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
}
input[type=file] {
    display: none;
}

 input[type=file]{
    display:none;
}
 .custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
}
<div class="form-group">
 <label for="before_image" class="custom-file-upload"><i class="fa fa-cloud-upload"></i> Before Image*</label>
 <input type="file" id="before_image" name="before_image" class="form-group" placeholder="Before Image" data-bind="value: before_image">                        <span>No file chosen</span>
</div>

UPDATE: I tried all these solutions (How to display file name for custom styled input file using jquery?) but none worked for me. In my scenario I have two upload buttons one is before image upload and second is after image upload as shown in video.

Still I can't display the preview text in front of the upload buttons.

Iftikhar uddin
  • 3,117
  • 3
  • 29
  • 48
  • Possible duplicate of [How to display file name for custom styled input file using jquery?](https://stackoverflow.com/questions/41542845/how-to-display-file-name-for-custom-styled-input-file-using-jquery) – Lalji Tadhani May 30 '19 at 10:33

2 Answers2

1

It does not work because your <span>No file chosen</span> is static.

Try:

<div class="form-group">
    <label for="before_image" class="custom-file-upload"><i class="fa fa-cloud-upload"></i> Before Image*</label>
    <input type="file" id="before_image" name="before_image" class="form-group" placeholder="Before Image" data-bind="value: before_image">                        
    <span id="images"></span>
</div>

<script type="text/javascript">
var field = document.getElementById('before_image');
var span = document.getElementById('images');

field.addEventListener('change', function() {
    var img = document.createElement('img');
    img.src = field.files[0].getAsDataURL();
    span.appendChild(img);
});
</script>
Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38
1

Just add a new function in input type file on onchange event. Please find the attached code.

 input[type=file]{
    display:none;
}
 .custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
}
<div class="form-group">
 <label for="before_image" class="custom-file-upload"><i class="fa fa-cloud-upload"></i> Before Image*</label>
  <input type="file" id="before_image" name="before_image" class="form-group" placeholder="Before Image" data-bind="value: before_image" onChange="((e)=>{
file = e.target.files.item(0);
var fr = new FileReader;
fr.onloadend =(imgsrc)=>{
  imgsrc = imgsrc.target.result; // file reader
var img = document.createElement('img');
    img.src =imgsrc;
this.nextSibling.nextSibling.innerHTML='';
this.nextSibling.nextSibling.appendChild(img);
};
fr.readAsDataURL(file);
})(event)">
<span>No file chosen</span>
</div>
localhost
  • 483
  • 4
  • 10