Reputation: 372
I want to access csv file from js.
So I upload it using POST form tag in html.
Than how could I access it using js?
Upvotes: 0
Views: 607
Reputation: 615
With simple ajax request you can do it. With raw java script you can access your file like this.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var responseText = xhttp.responseText; //The responseText is the content of your file.
}
};
xhttp.open("GET", "/directory-of-uploaded-files/filename", true);
xhttp.send();
And with jquery
$.ajax({
url: "/directory-of-uploaded-files/filename",
method: "get",
cache: false,
success: function(file_content){
console.log(file_content); //file_content is the content of your file.
},
error: function (e) {
alert("Error");
}
});
Upvotes: 2
Reputation: 1300
You can't without making AJAX calls. Once it's on the server, the client will need to request it.
What you're asking for is a core concern with web development, and how you're going to be able to accomplish it is heavily dependent on what your server is running. For example, if you've posted this CSV file to a server running PHP and MySQL, and it's now stored in a MySQL database, you'll need server-side PHP for retrieving the file from the database and providing its contents to the client.
I hope this helps.
Upvotes: 0