使用javascript的execCommand('SaveAs', false, filename);功能
编辑:不再工作。由于浏览器的安全性考虑,这个Javascript函数过去可以在所有浏览器上工作,但现在只在IE上工作。它向通过浏览器运行此功能的用户提供了“另存为”对话框,用户按OK键,该文件由服务器端的javascript保存。
现在这个代码是一个罕见的古董零日收藏品。// 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;}
像这样调用函数:msg = "I am the president of tautology club.";save_content_to_file(msg, "C:\\test");