Save file from local data in Javascript

Here is the scenario:

  • A user comes to my site and opens a web page with some javascript features.
  • User edits data through javascript
  • The user presses the save button to save the data. The fact is that they do not need to download this data because they are already in javascript on the local computer.

Is it possible to save data from javascript (executed from someone else's web page) without downloading a file from the server?

Any help would be greatly appreciated!

+5
source share
5 answers

, - , , , Downloadify, JavaScript + Flash " " ...

.

+5

, . jQuery, /div Blob, Blob. .

jQuery.fn.downld = function (ops) {
  this.each(function () {
    var _ops = ops || {}, 
    file_name = _ops.name || "downld_file",
    file_type = _ops.type || "txt",
    file_content = $(this).val() || $(this).html();

    var _file = new Blob([file_content],{type:'application/octet-stream'});
    window.URL = window.URL || window.webkitURL;

    var a = document.createElement('a');
    a.href = window.URL.createObjectURL(_file); 
    a.download = file_name+"."+file_type;
    document.body.appendChild(a);
    a.click(); $('a').last().remove();
  });
}

: $( "# element" ). downld(); : $( "# element" ). downld ({name: "some_file_name", type: "html" });

Codepen http://codepen.io/anon/pen/cAqzE

+5

JavaScript sandboxed, . , (-, javascript ..). , (I/O, ), .

, , , ​​ flash, java- Silverlight. ( 2, , , ...)

+3

/ 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>

<script type="text/javascript">
    try {window.close ();} catch (e) {alert (e);}
</script>
</BODY>
</HTML>

//

/ , - .

+1

cookie.

0
source

All Articles