[英]How to write a local .txt with Javascript
I want to write a .txt file using the javascript console in IE. 我想使用IE中的javascript控制台编写.txt文件。 I already have the strings that I would like to write on the file; 我已经有想要写在文件上的字符串了; so, what I would like to do is: 因此,我想做的是:
var refTab=document.getElementById("historymatch_tb0");
var ttl;
for ( var i = 0; row = refTab.rows[i]; i++)
{
row = refTab.rows[i];
for ( var j = 0; col = row.cells[j]; j++ )
{
WriteInFile (col.innerHTML);
}
}
What I don't have is the function WriteInFile because I triyed this: 我没有的是功能WriteInFile,因为我尝试了以下方法:
function WriteInFile (a)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filename = "c:\\Users\\Riccardo\\a.txt";
var f = fso.OpenTextFile(filename, 2, true);
f.WriteLine(a);
f.Close();
}
this doesn't works, The script doesn't give me errors but the file is empty and The console shows the word: undefined! 这不起作用,脚本不会给我错误,但文件为空,控制台显示单词:undefined! Where is the problem? 问题出在哪儿? Thanks! 谢谢!
Since HTML5 this is possible. 由于HTML5,这是可能的。 See http://www.w3.org/TR/file-writer-api/#the-filesaver-interface and http://eligrey.com/blog/post/saving-generated-files-on-the-client-side 参见http://www.w3.org/TR/file-writer-api/#the-filesaver-interface和http://eligrey.com/blog/post/saving-generation-files-on-the-client-侧
If I am understanding you correctly, what you are really asking is for a way to persist information in js between sessions. 如果我理解正确,那么您真正要问的是一种在会话之间将信息持久化在js中的方法。 I understand how a file looks great for that task. 我了解文件对于该任务的外观如何。 But since browsers have a very limited access to the filesystem, that is not ideal. 但是由于浏览器对文件系统的访问非常有限,因此不理想。
Instead of using a local file, you can persist the information you need in the browser's Web Storage. 您可以将所需的信息保留在浏览器的Web存储中,而不必使用本地文件。 You can come later and retrieve it. 您可以稍后再取回。 See http://www.html5rocks.com/en/features/storage for details 有关详细信息,请参见http://www.html5rocks.com/en/features/storage
Maybe it's some browser issue ? 也许是浏览器问题? I tried in IE9 - it works correctly (see jsfiddle - it's your code) 我在IE9中尝试过-它正常工作(请参阅jsfiddle-这是您的代码)
var a = "Hello";
WriteInFile(a);
http://jsfiddle.net/nb5N7/1/ http://jsfiddle.net/nb5N7/1/
But, also I must enable not safe activeX execution 但是,我还必须启用不安全的ActiveX执行
http://social.msdn.microsoft.com/Forums/vstudio/en-US/41b3a68b-dd16-48a8-b86c-02a1e543081c/activexobjectscriptingfilesystemobject-automation-server-cant-create-object (see answer) http://social.msdn.microsoft.com/Forums/vstudio/zh-CN/41b3a68b-dd16-48a8-b86c-02a1e543081c/activexobjectscriptingfilesystemobject-automation-server-cant-create-object (请参阅答案)
UPD: why you are minused me ? UPD:为什么你让我失望? UPD2: UPD2:
for ( var i = 0; row = refTab.rows[i]; i++)
maybe possible issue here ? 也许这里有问题? you should write 你应该写
var i = 0; i < refTab.rows.lenght; i++
instead of 代替
for ( var i = 0; row = refTab.rows[i]; i++)
and, also, try add alert(1); 并且,还尝试添加alert(1); to for cycle (to check is it work correctly) 到周期(检查它是否正常工作)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.