Javascript D3-fetch write to csv - javascript

i have some code that reads a csv using d3-fetch. I load my csv in the way:
D3.csv(./mydata.csv).then(function (info) {
Console.log(info);
});
this works fine and gives me my data. But how would i write to this data?

Keep in mind D3 is supposed to be run in a web browser. Web browsers can read files through HTTP but they cannot write files.
A server could write files on a file system it controls. So you would need to write a server endpoint like /editCSV and then make a POST request to it from the browser. Your server could then write the changes to the CSV file.
In order to do this, you need to have control of the server (for example, a Node.js server) that is serving the CSV and the related file.

Related

Javascript: Save uploaded file

I would like to save uploaded file using javascript, in my linux server. The code I wrote is:
if (uploadInput.files.length == 0) {
console.log("No file is uploaded. ");
} else {
console.log("File uploaded.");
var file = uploadInput.files[0];
}
Now I would like to save that file as "files/upload.csv". Can anyone please advise, how can I do it?
Thanks in advance
What I'm going to do is walk you through the logic, instead of providing code. There is just not enough information here on what you want to do to provide actual code and the sample you provided is a very small part of what the actual solution would need to include.
I'm assuming the code you wrote above is meant to run on a website visitor's browser (client-side). Client-side code can't save to a server. What it can do, is send the file contents to the server. But then you'd need something on the server side to process that file contents and actually save it to the server-side files directory.
One method to send the file contents from the client to the server is to use AJAX - you can do this with native javascript, but I would recommend looking into a library such as Jquery, which makes it a lot easier. See http://api.jquery.com/jquery.ajax/ This AJAX code will need a communication point on the server to send the file contents to. From your profile it seems you're familiar with PHP. You could make a php file on the server (say receivefilecontents.php) that takes in input from that client-side AJAX call, and then saves it to a server directory - you could also do this in Python, Java or a number of other languages.

How can i read local file from D drive in javascript

My requirement is to read the json file which contains some data and store in some other .js file.
I got task to read local file from local disk in Javascript , i have used file path like - D:\json\analytics.json.
(document).ready(){
($).getData("D:\json\analytics.json");
}
when i see in firebug it takes other url.
How I can do it, is it possible to read file from javascript.
I don't know javascript , i have seen some answer but i am not able to understand .
Need Solution , how I can achieve it. is there any other way to read file on jsp without using scriptlet . From server side , can send it on the jsp page.
I think Jaronmanda's answer won't work cause it will hit cross origin issue, see "Cross origin requests are only supported for HTTP." error when loading a local file.
As the page suggested, in general you need to serve that json file from a web service (same domain, or allow your domain to access), but it depends on what you really need to do. If you can control where that json file is stored, the easier way is to put that in a subdirectory of your html file, and do:
$(document).ready(function () {
$.get('<directory>/analytics.json', function (data) {
// Do your stuff
});
});

JavaScript googlemaps read coordinates from file

I want to display some markers using googlemaps. The information (coordinates) are stored in a local *.csv file (wich I want to use a "ressource-file").
How can I read this *.csv file? If I use "jQuery.get('myFile.csv', function (data) {..." it dosn't work.
The error message is: Cross origin requests are only supported for protocol schemes
Do I hava to make a file selection to read the file? Is there no other way?
Thanks
Is it possible that you are trying to load data from a file and not a running server(for example by double-clicking the .html from your file manager)?
If your are on the file:// protocol (which you can see in your url) this will not work. You could try changing to the development directory and runnig python3 -m http.server which will start a small development server. You can than change to http://localhost:8000 and see if it works.

CSV is too large to be processed via AJAX

I'm having a problem when sending large amounts of data through an AJAX request. I'm pulling in an XLS file from a website and attempting to pass it through an API by parsing the data. I'm doing this in VBScript/Classic ASP so there is no native function to parse XLS so I'm first attempting to convert it to a CSV file through Javascript.
I'm using something called SheetJS (http://oss.sheetjs.com/js-xls/) which is a great tool and it works just as I need it to. I can run an Excel file through it and it outputs the correct CSV data. I then try to send that data via AJAX to the ASP page with my code and I get a 500 error that I've isolated to being an issue with the file being too large. I was able to isolate to about 1652 lines of my Excel file and anything past that generates a CSV file too large to send.
All I am getting is a 500 error so I'm not really sure what else to do from this point. Is there a data limit on AJAX functions? Or is it a time limit type issue? I don't know how to find out which it is. Any suggestions on how to get a more detailed error message AND any fixes for this issue?
While you can't work with .xls files natively in VBScript, there is ADO that makes it easy to do that via COM. This way scales well wrt memory. Start your research here.

How to access a local JSON file with Javascript?

I have a Python program that generates an html page for reporting results. The html page is saved in an output directory on disk alongside a javascript file that helps with dynamic table handling. I also save a JSON file to this output directory that I would like to read in with my javascript file. This JSON file has data from the Python run (saved dictionary) that I would like to be able to access. So in an output directory on disk I have:
C:/somedirectory/output/report.html
C:/somedirectory/output/tables.js
C:/somedirectory/output/data.json
All files have been created from my program.
My html page has a table with checkboxes and if those checkboxes are selected I would like to update a second table based on data saved in the JSON file. Thus I would like to open my html report in any browser and read in the JSON file as a javascript object.
I have been trying to use ajax and .getJSON but am getting the
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
I have searched and seen many similar problems but have not come across anything that quite fits what I need. Thoughts and a work around would be greatly appreciated. Thanks.
Update
Since everything is run locally on the client side I have decided to embed the JSON data (python dictionary) and javascript code directly into the html report output. This way the data is internally accessible and the html file can be passed around without dependency issues. The user with the answer I selected below has a link that eludes to this solution.
JavaScript runs on the client machine, hence it can only access files on the client machine using a special setup.
If you want it to read JSON on your server, you should use the path:
http://example.com/output/data.json
Better way would be to read/write JSON file from Python and then send the table data to JavaScript as in this answer: Send data from Python to Javascript (JSON)

Categories

Resources