How can we download data in tables in a html page into a csv file using
client side Javascript. I should click a button and a table's data
should be downloaded into a csv in local drive. The os used is m$
windows..
The simplest option is to perhaps create the csv on the server, and ask
the client to go to a download page and save the csv file themselves.
If you want to do this on the client side, then:-
The creation of the csv string can be done using JavaScript and the
HTML DOM, by cycling through the rows and cells collections of the
TABLE object.
You could then insert the results in a TEXTAREA and let the client cut
and paste them into a text editor.
If you want JavaScript to sav the csv string to the client's local
disk, this is a different matter.
JavaScript does not natively have any local file handling capability.
However, if the client is running Internet Explorer, then the client is
likely to have the Scripting.FileSystemObject ActiveX COM object
installed, which JavaScript may be able to access using:-
var fileSystem = new ActiveXObject("Scripting.FileSystemObject").
Firefox also has file handling components based on XPCOM.
However, whether these components will work will obviously depend on
the client security settings. If the client's browser has blocked
ActiveX objects, then you will not be able to do this without the
client granting the appropriate permissions. There is no way round
this, for the obvious reason that unrestricted access to a file system
would be a danagerous thing on the internet.
Regards
Julian