Last active
June 5, 2023 14:48
-
-
Save tanvir002700/c65f5c6608c16540a72688f011cf2485 to your computer and use it in GitHub Desktop.
Upload CSV file with Jquery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>CSV upload</title> | |
</head> | |
<body> | |
<input type="file" name="filename" id="filename"> | |
<button id="upload">upload</button> | |
<div class="csv"></div> | |
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha256-/SIrNqv8h6QGKDuNoLGA4iret+kyesCkHGzVUUV0shc=" crossorigin="anonymous"></script> | |
<script src='app1.js'></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this is the 1st way. | |
$(document).ready(function(){ | |
$('#upload').click(function(){ | |
var csv = $('#filename'); | |
var csvFile = csv[0].files[0]; | |
var ext = csv.val().split(".").pop().toLowerCase(); | |
if($.inArray(ext, ["csv"]) === -1){ | |
alert('upload csv'); | |
return false; | |
} | |
if(csvFile != undefined){ | |
reader = new FileReader(); | |
reader.onload = function(e){ | |
csvResult = e.target.result.split(/\r|\n|\r\n/); | |
$('.csv').append(csvResult); | |
} | |
reader.readAsText(csvFile); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//2nd way upload csv with jquery. | |
$("#filename").change(function(e){ | |
var ext = $("#filename").val().split(".").pop().toLowerCase(); | |
if($.inArray(ext, ["csv"]) == -1) { | |
alert('Upload CSV'); | |
return false; | |
} | |
if(e.target.files != undefined){ | |
var reader = new FileReader(); | |
reader.onload = function(e){ | |
csvResult = e.target.result.split(/\r|\n|\r\n/); | |
$('.csv').append(csvResult); | |
} | |
reader.readAsText(e.target.files.item(0)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment