9

I would like to make custom button for an image upload. I am able to get this result with the demo below:

https://jsfiddle.net/algometrix/fgrbyo4z/

But HOW CAN I DISPLAY the file name selected after? Or maybe even a thumbnail of an image if possible? Like after I choose a file from the window that pops open I would like it to display 'the file name' on the page after I selected it.

Javascript - jQuery is totally an option here if anyone can help in that area.

HTML

<div>
  <div style="display:block;text-align:center;margin-top:20%;">
    <label for="files"> <span class="btn">Select Image</span></label>
    <input style="visibility: hidden; position: absolute;" id="files" class="form-control" type="file" name="files">

  </div>

</div>

CSS

.btn {
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  background: #3f88b8;
  padding: 10px 20px 10px 20px;
  text-decoration: none;
}

.btn:hover {
  background: #3cb0fd;
  background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
  background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
  text-decoration: none;
}
Papa De Beau
  • 3,416
  • 17
  • 75
  • 132

3 Answers3

8

With the addition of javascript, we can just watch for the change event on the input, and put the name on the page. Please note these minor HTML changes:

<div class="file-upload">
  <label for="upload-1" class="btn">Upload</label>
  <input type="file" id="upload-1">
  <p class="file-name">Please select a file.</p>
</div>

With this jQuery:

jQuery(function($) {
  $('input[type="file"]').change(function() {
    if ($(this).val()) {
         var filename = $(this).val();
         $(this).closest('.file-upload').find('.file-name').html(filename);
    }
  });
});

Working Fiddle

random_user_name
  • 23,924
  • 7
  • 69
  • 103
3

Just tap into the change event of the file element:

 var file = document.getElementById("files");
 file.addEventListener("change", function(){ alert(this.value); });

Shown in this fiddle: https://jsfiddle.net/pbg44bqu/12/

Scott Marcus
  • 57,085
  • 6
  • 34
  • 54
1

This script shows the image as soon as uploaded:

function readURL(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#img').show().attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#files").change(function(){
    readURL(this);
});

Source

Updated Fiddle

Community
  • 1
  • 1
WcPc
  • 457
  • 2
  • 11
  • Thanks WcPc, I have one quick question. How can I send this image through a form I have set up? Like can I have javascript add this file to my form data and then when I submit the form can I then send this image as data with my current form? – Papa De Beau Mar 08 '16 at 18:26
  • You do not need js for this, just add the form tags around it: `
    ` and then in your PHP (I assume you use PHP): `var your_file = $_POST['control_name']` for example `var your_file = $_POST['files']` Even though you show the image already it is still selected in your fileupload control...
    – WcPc Mar 10 '16 at 20:12