File upload via javascript?

So today I thought. I generate CSV from the data already in memory in my JavaScript application, then I pump this data into a user browser through a file download hint - all in JavaScript -.

Is it possible?

+3
source share
3 answers

If you want to do this completely on the client side, without any interaction with the server, you will need at least Flash help.

Take a look at Downloadify , this is the best I've seen to create files on the client side.

Mark the demo .

+2
source

/ javascript . smartclient-html-jsp.

:

  • SmartClient. / ( ).
  • - RESTish . URL-; / .
  • JSP, : blank.jsp export.jsp.
  • blank.jsp , .
  • , ( ), :   . url blank.jsp   . using document.write .   . POST, export.jsp .   . export.jsp, , .

//

<%@ 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>

//

/ , - .

+1

MIME, location.href = 'myFile.csv'.

.

, .

0

Source: https://habr.com/ru/post/1752664/


All Articles