Read/write to file using jQuery

后端 未结 7 1051
迷失自我
迷失自我 2020-11-22 13:29

Is there a way to get jQuery to get information to and from a file? Is it possible? How?

相关标签:
7条回答
  • 2020-11-22 13:40

    both HTML5 and Google Gears add local storage capabilities, mainly by an embedded SQLite API.

    0 讨论(0)
  • 2020-11-22 13:42

    If you want to do this without a bunch of server-side processing within the page, it might be a feasible idea to blow the text value into a hidden field (using PHP). Then you can use jQuery to process the hidden field value.

    Whatever floats your boat :)

    0 讨论(0)
  • 2020-11-22 13:54

    You will need to handle your file access through web programming language, such as PHP or ASP.net.

    To set this up, you would:

    • Create a script that handles the file reading and writing. This should be visible to the browser.

    • Send jQuery ajax requests to that script that either write data or read data. You would need to pass all of your read/write information through the request parameters. You can learn more about this in the jQuery ajax documentation.

    Make sure that you sanitize any data that you are storing, since this could potentially be a security risk. However, this is really just standard flat-file data storage, and is not necessarily that unusual.

    As Paolo pointed out, there is no way to directly read/write to a file through jQuery or any other type of javascript.

    0 讨论(0)
  • 2020-11-22 13:57

    No, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least. If you wanted to get/store information server-side, though, you can certainly make an Ajax call to a PHP/ASP/Python/etc. script that can then get/store the data in the server. If you meant store data on the client machine, this is impossible with JavaScript alone. I suspect Flash/Java may be able to, but I am not sure.

    If you are only trying to store a small amount of information for an unreliable period of time regarding a specific user, I think you want Web Storage API or cookies. I am not sure from your question what you are trying to accomplish, though.

    0 讨论(0)
  • 2020-11-22 13:57

    Yes it is possible.

    The technique is described below

    http://jquery.tiddlywiki.org/twFile.html

    0 讨论(0)
  • 2020-11-22 13:59

    Use javascript's execCommand('SaveAs', false, filename); functionality

    Edit: No longer works. This Javascript function used to work across all browsers, but now only on IE, due to browser security considerations. It presented a "Save As" Dialog to the user who runs this function through their browser, the user presses OK and the file is saved by javascript on the server side.

    Now this code is an rare antique zero day collectible.

    // content is the data (a string) you'll write to file.
    // filename is a string filename to write to on server side.
    // This function uses iFrame as a buffer, it fills it up with your content
    // and prompts the user to save it out.
    function save_content_to_file(content, filename){
        var dlg = false;
        with(document){
         ir=createElement('iframe');
         ir.id='ifr';
         ir.location='about.blank';
         ir.style.display='none';
         body.appendChild(ir);
          with(getElementById('ifr').contentWindow.document){
               open("text/plain", "replace");
               charset = "utf-8";
               write(content);
               close();
               document.charset = "utf-8";
               dlg = execCommand('SaveAs', false, filename);
           }
           body.removeChild(ir);
         }
        return dlg;
    }
    

    Invoke the function like this:

    msg =  "I am the president of tautology club.";
    save_content_to_file(msg, "C:\\test");
    
    0 讨论(0)
提交回复
热议问题