How to extract filename from a file input control - jQuery
VIDEO
jQuery change() method can be used to get the file name, file type, file size selected by the HTML form control <input type="file"> . Let's check out an example to understand how it works:
HTML
<!DOCTYPE html>
<html >
<head >
<title > Jaya Krishna Miriyam</ title >
<link rel = "stylesheet" href = "https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity= "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin= "anonymous" >
<script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity= "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin= "anonymous" ></ script >
<script src = "https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity= "sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin= "anonymous" ></ script >
<script src = "https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity= "sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin= "anonymous" ></ script >
<script src = "https://kit.fontawesome.com/1a2b0738bb.js" crossorigin= "anonymous" ></ script >
</ head >
<body >
<div class = "main-container" >
<form >
<div class = "row" >
<div class = "col-12 bg-light" >
<div class = "icons text-center p-3" >
<a href = "https://www.instagram.com/jayakrishna.miriyam/" class = "text-dark" >
<i class = "fa-brands fa-square-instagram" ></ i >
</ a >
<a href = "https://www.youtube.com/channel/UCxiXTvcR3z8JSYY6ycuXmbg?sub_confirmation=1" class = "text-danger" >
<i class = "fa-brands fa-youtube" ></ i >
</ a >
<a href = "https://www.facebook.com/jayakrishna.miriyam/" class = "" >
<i class = "fa-brands fa-square-facebook" ></ i >
</ a >
</ div >
</ div >
<div class = "col-12 mt-5" >
<h4 class = "text-center font-weight-bold text-dark" > Upload Your Files</ h4 >
</ div >
<div class = "col-12" >
<div class = "my-4 text-center" >
<input type = "file" multiple id = "fu" / >
</ div >
</ div >
</ div >
<div class = "container" >
<table id = "tableName" class = "table d-none" >
<thead class = "thead-light" >
<tr >
<th scope = "col" > File Name</ th >
<th scope = "col" > File Type</ th >
<th scope = "col" > File Size</ th >
</ tr >
</ thead >
<tbody id = "list" >
</ tbody >
</ table >
</ div >
</ form >
</ div >
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" ></ script >
<script >
$('#fu').on('change', function() {
$('#list').empty();
var fu = $('#fu');
var lengths = fu[0].files.length;
var items = fu[0].files;
var frag = "";
$('#tableName').removeClass('d-none');
if (lengths > 0) {
for (var i = 0; i < lengths; i++) {
var fileName = items[ i] .name ;
var fileSize = items[ i] .size ;
var fileType = items[ i] .type ;
frag = "<tr><th> " + fileName + "</ th > <th > " + fileType + "</ th > <th > " + fileSize + " bytes</ th ></ tr > ";
$('#list').append(frag);
}
}
})
</ script >
</ body >
</ html >
CSS
@import url('https://fonts.googleapis.com/css2?family=Bree+Serif&family=Caveat:wght@400; 700&family=Lobster&family=Monoton&family=Open+Sans:ital,wght@0,400; 0,700;1,400;1,700&family=Playfair+Display+SC:ital,wght@0,400; 0,700;1,700&family=Playfair+Display:ital,wght@0,400; 0,700;1,700&family=Roboto:ital,wght@0,400; 0,700;1,400;1,700&family=Source+Sans+Pro:ital,wght@0,400; 0,700;1,700&family=Work+Sans:ital,wght@0,400; 0,700;1,700&display=swap');
.main-container {
height: 100vh;
}
.icons {
font-size: 30px;
}
.icons a {
margin-right: 15px;
}
0 Comments