3

So I had a thought today. I generate CSV from data already in memory in my JavaScript application, then pump that data to the user's browser via a File download prompt -- all in JavaScript --.

Is this possible?

4
  • What is the context of this Javascript application? Where is it running, a web server or a browser?
    – Pointy
    Commented Jul 1, 2010 at 15:29
  • 2
    This is only possible on a certain webbrowser developed by a team in Redmond which is using a proprietary framework cluttered with security holes called "ActiveX". Even then, I think you'll get warnings when doing so in the more recent versions of that webbrowser. In all the other webbrowsers the world is aware of, this is not possible.
    – BalusC
    Commented Jul 1, 2010 at 15:32
  • @BalusC, it's also possible with the help of Flash, on any browser. Commented Jul 1, 2010 at 17:08
  • @CMS: ActionScript is not JavaScript, where this question was all about :) But you're right, it's possible with Flash. Not JavaScript.
    – BalusC
    Commented Jul 1, 2010 at 17:13

3 Answers 3

2

If you want to do it entirely on the client-side, without any server interaction, you will need at least the help of Flash.

Give a look to Downloadify, is the best I've seen for client-side file generation.

Check the demo.

1
  • @BalusC, thanks!, you are right, it's simply a JavaScript proxy API to ActionScript. Commented Jul 1, 2010 at 17:24
1

The solution to download local/client-side contents via javascript is not straight forward. I have implemented one solution using smartclient-html-jsp.

Here is the solution:

  1. I am in the project build on SmartClient. We need to download/export data of a grid (table like structure).
  2. We were using RESTish web services to serve the data from Server side. So I could not hit the url two times; one for grid and second time for export/transform the data to download.
  3. What I did is made two JSPs namely: blank.jsp and export.jsp.
  4. blank.jsp is literally blank, now I need to export the grid data that I already had on client side.
  5. Now when ever user asks to export the grid data (by clicking a link), I do below: a. Open a new window with url blank.jsp b. using document.write I create a form in it with one field name text in it and set data to export inside it. c. Now POST that form to export.jsp of same heirarchy. d. Contents of export.jsp I am pasting below are self explanatory.

// code start

<%@ page import="java.util.*,java.io.*,java.util.Enumeration"%>
<%
    response.setContentType ("text/csv");
    //set the header and also the Name by which user will be prompted to save
    response.setHeader ("Content-Disposition", "attachment;filename=\"data.csv\"");
    String contents = request.getParameter ("text");
    if (!(contents!= null && contents!=""))
        contents = "No data";
    else
        contents = contents.replaceAll ("NEW_LINE", "\n");

    //Open an input stream to the file and post the file contents thru the
    //servlet output stream to the client m/c

    InputStream in = new ByteArrayInputStream(contents.getBytes ());
    ServletOutputStream outs = response.getOutputStream();

    int bit = 256;
    int i = 0;
    try {
        while ((bit) >= 0) {
            bit = in.read();
            outs.write(bit);
        }
        //System.out.println("" +bit);
    } catch (IOException ioe) {
        ioe.printStackTrace(System.out);
    }
    outs.flush();
    outs.close();
    in.close();
%>
<HTML>
<HEAD>

</HEAD>

<BODY>


</BODY>
</HTML>

// code end

This code is tested and deployed/working in production environment, also this is cross-browser functionality.

Thanks Shailendra

2
  • Google has added some native File API access in Chrome too.
    – Drew
    Commented Jun 23, 2011 at 15:54
  • Hey @Drew, Are those API specific to Chrome browser? Mine are browser independent and also no problem on IE which stops auto script execution by applying internet security.
    – shaILU
    Commented Jul 22, 2011 at 20:27
0

If your server is setup to trigger a download for your given MIME type then you can simply do a straight location.href = 'myFile.csv'.

This will prompt the user to open or save the file as you'd like.

This does require that your server be configured to behave this way.

Not the answer you're looking for? Browse other questions tagged or ask your own question.