Read start of CSV file with JS/jQuery

Associate
Joined
3 Jan 2006
Posts
522
Location
The Undercroft
I'm currently looking into the possibilities of a web based data import app and slightly stuck at the first hurdle.
Say the client has a CSV text file with some 5000+ records to be imported to a DB on the server, can I use javascript/jQuery to read only the first few lines to verify it is indeed a CSV file and get the structure of the data before uploading the entire file to the server?
I've been googling it and not found anything helpful, is JS capable of this?
How do you do partial file uploads?
It will be a C#.net MVC app. No flash!
Any help appreciated.

Edit: I realise a 5000 line txt file isnt that big and can be uploaded straight off and be processed server side but theres the potential for files containing 50k+ records over a slow wireless connection.
 
Last edited:
Here's a jQuery plugin to turn CSV output into an array - http://plugins.jquery.com/project/csv

However, AFAIK you can't do partial file uploads or any testing of the file on the client side (other than to check file extension), therefore the only way to do what you want is to completely upload the file to the server and then run your checks on it to ensure it's a properly formed CSV.

You could do it via some form of AJAX upload to smooth the process a little more, however you're still going to have the bottleneck of uploading a 50k line CSV
 
Yeah thats what I suspected. I guess we can just upload the smaller files and deal with the odd larger file another way.
 
Hi,

One way you could do this is to write a service on the server that will give you the a description of what the file to be imported should look like e.g number of fields per line, separator etc. That could be sent back to the JS in a JSON structure etc. As it's unlikely to change that could probably be cached so that you're only requesting it once.

Then, before submitting the CSV file, presumably using a POST, you can check it against the pattern received from the server.

Or if it's a REST type service you're using maybe the GET operation could retrieve you the pattern for validation with the POST operation used to send the data.

Actually checking the legality of fields etc. is something that should be done on the server side, but you could still do some checking on the client to make sure you're not going to be uploading complete rubbish.

Hope that's useful.

Jim
 
Nice idea JIMA but there isnt a fixed layout for the source file - it could have any number of fields and will be mapped to the destination table by the user.
I'll just do all file validating/processing server-side, less of a headache
 
Back
Top Bottom