0

i am using a custom file input on an upload page on my website and it is working as per my requirement the only issue is i have hidden the default layout of filetype="input" but i want to show the name of the file being uploaded so that the user may know which file he has uploadedand the name of the file here's the fiddle

JsFiddle

here's the html and css

<div class="custom-upload">
  <div class="fake-file">
   <input placeholder="Choose File" type="file" name="fileToUpload" id="fileToUpload" />
  </div>
 </div>

.custom-upload {
    position: relative;
    height: 40px;
    width: 100%;
    margin-top: 20px;
    border-bottom: 1px solid #625f5b;
}

.custom-upload input[type=file]
{
    outline:none;
    position: relative;
    text-align: right;
    -moz-opacity:0 ;
    filter:alpha(opacity: 0);
    opacity: 0;
    z-index: 2;
    width:100%;
    height:100%;

}

.custom-upload .fake-file
{
    background:url(https://s4.postimg.org/hy3g354ot/upload.png) center right no-repeat;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    padding: 0;
    margin: 0;
    z-index: 1;
    line-height: 100%;
}

.custom-upload .fake-file input
{
    font-size:16px;
    height:40px;
    width: 100%;
}
4
  • Is this enough: jsfiddle.net/tk27sk6q/4 Commented Aug 24, 2016 at 15:34
  • yea thats great but how will i show it to a hidden field? Commented Aug 24, 2016 at 15:40
  • Do you mean you have a hidden input and want to put the name of the file as value? Commented Aug 24, 2016 at 15:41
  • exactly! the type="file" is hidden because i dont want to show that creepy choose file button but i still want the name to be displayed as value Commented Aug 24, 2016 at 15:44

2 Answers 2

2

Look at the JavaScript I added.

Note: I used jQuery. If you are using native JavaScript, I have to change the code

$(function(){
  $("#fileToUpload").on('change',function(){
    fpath = $(this).val();
    $('#filePath').html('<b>You selected the file:</b> ' + fpath);
  });
});
.custom-upload {
    position: relative;
    height: 40px;
    width: 100%;
    margin-top: 20px;
    border-bottom: 1px solid #625f5b;
}

.custom-upload input[type=file]
{
    outline:none;
    position: relative;
    text-align: right;
    -moz-opacity:0 ;
    filter:alpha(opacity: 0);
    opacity: 0;
    z-index: 2;
    width:100%;
    height:100%;

}

.custom-upload .fake-file
{
    background:url(https://s4.postimg.org/hy3g354ot/upload.png) center right no-repeat;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    padding: 0;
    margin: 0;
    z-index: 1;
    line-height: 100%;
}

.custom-upload .fake-file input
{
    font-size:16px;
    height:40px;
    width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-upload">
  <div class="fake-file">
    <input placeholder="Choose File" type="file" name="fileToUpload" id="fileToUpload" />
  </div>
  <div id="filePath">
  
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

@uneebmeer, (thumbsup)
0

Another choise could be without using jquery, but pure javascript. This is my solution.

<span id='filename'></span>
<div class="custom-upload">
  <div class="fake-file">
    <input placeholder="Choose File" type="file" name="fileToUpload" id="fileToUpload" />
  </div>
</div>
<script>
    document.getElementById('fileToUpload').addEventListener('change', function() { 
    var dest = document.getElementById('filename');
    dest.innerHTML = document.getElementById('fileToUpload').value;
  });
</script>

I let you the personalization of css of the elements to fit your needs. Hope this help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.