Getting CSV Columns' name using JavaScript - javascript

I must upload large files (+1GB) in CSV format. I must check the column names, and I want to do it before upload process. So, I am trying to read the CSV using JavaScript, following
http://www.html5rocks.com/en/tutorials/file/dndfiles/
and
https://developer.mozilla.org/en-US/docs/Web/API/FileReader.readAsBinaryString
It work! But I have to read the complete file, and believe me, the files are too large.
My question is: How can I get the CSV columns' name using JavaScript, without reading the entire file?

You can access all kinds of data about a file without passing it to a FileReader. You can get the size of a file by checking file.size.
Check the documentation for File and Blob (File inherits Blob) to see what else is available to you.

Related

I have a separate .json file, I need to iterate over the file or read through the file and do some stuff. How exactly do I do this in Javascript?

I have a folder with an html, css, json, and js file. The js file are a list of person's and what they do. I need to analyze the json file contents using javacript. I know that in C or C++ you can open the file for reading and analyze it, then close it. So, how do I do this in Javascript? And do I need to add the name of the JSON array in the .json file, or should i leave the contents as it is?
JSON screenshot image
Nothing online have worked thus far. Hopefully Stackoverflow can help.
Let's assume you're using nodeJS, you can import the json file using require .
After you've imported the json file, you can access the object as it would be a JS object.
const jsonFile = require("./pathtofile.json")
for(let obj in jsonFile) {
console.log(obj)
}

How to update zip file with XML content

Overview
I am using JSZip which allows you to create and update zip files using JavaScript. I would like to update some of the contents of the zip file with other files. However the examples in the documentation only show how to use strings.
In my situation I have an XML file and I want to overwrite another XML file within the Zip file. According to the documentation the file() method supports multiple types:
file(name, data [,options])
String/ArrayBuffer/Uint8Array/Buffer/Blob/Promise/Nodejs stream
Question
I have access to my zip file and my XML file is local as it will be built pro-grammatically. How can I go about using this method to overwrite files with XML files. I also expect the need to overwrite images as well but I also didn't see examples of this either.
Documentation - https://stuk.github.io/jszip/
If I do an import of the XML:
import testXML from '../testXML';
This will not work as when I console log this, I get:
static/media/testXML.dd632dc5.xml
Note: I am using React for the frontend if this matters.

How to store config information for a ASP.NET site not in Web.Config

I need to store a file pairing colours and images for use in my JavaScript. I would have liked to use a simple CSV file and Papa Parse, but PP requires either text or a File object as input, and I can find no way of opening a File object, nor of reading the text from the CSV file. Surely my code should be allowed to read files that reside under the web site, not randomly among the file system?
My alternative is to have the end user, non-technical, edit a JSON file that is parsed by my code.
Am I wrong, or is this the case? Then maybe I should build an editor for the JSON file that simplifies the data editing for the end user.
All the file has to store is colour/image name pairs.
Have you tried using something like this? Are you getting an error? Sorry, I cannot leave comments yet.
StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true);
_testData.WriteLine(TextBox1.Text); // Write the file.
_testData.Flush();
_testData.Close(); // Close the instance of StreamWriter.
_testData.Dispose(); // Dispose from memory.

How do you check whether a CSV file is corrupted in PHP and/or Javascript?

I'm making an HTML5/jQuery/PHP app which involves uploading CSV files (via a drag and drop), and processing them to a MySQL database. So far, I can upload the files, and I know how to read them into a database.
My question: I am wondering if it is possible to detect whether a CSV file is in a corrupted format by PHP or Javascript/jQuery? For example, I can rename somefile.png (an image) to somefile.csv, and it still gets uploaded. If I open up the renamed file in Notepad++, all I see is garbage, which is expected.
I would like to do this on the clientside, so I can alert the user (via JQuery) whether the file is in a corrupted format. I'd also like to check on the serverside (via PHP) before I start iterating over each CSV file for db processing.
My first thoughts would be to use regular expressions, but I am unsure how to make ones for this particular problem. I know the basics of regular expressions, but haven't really used them in advanced settings before.
First of all you should check content-type of picked file, it should be "text/csv". At the server-side you can check file via fgetscsv PHP function (http://php.net/manual/function.fgetcsv.php) (catch null or false on error)
You don't want to be validating it if you're going to be reading it right after. Just read it in and catch any errors as you read. That way you come to know whether file is valid or corrupt.

how to read csv file from server using javascript

I am trying to read data from this online database http://coxpresdb.hgc.jp/top_search.shtml#CoExSearch, under CoExSearch. When I enter an id, says 1717, it will give a .csv file. How can I use javascript to read that csv file directly ( just to initalize an array).
Thanks.

Categories

Resources