Read/write to file using jQuery

后端 未结 7 1054
迷失自我
迷失自我 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: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");
    

提交回复
热议问题