admin管理员组文章数量:1289548
can any body tell me how i create a .txt file using javascript which is browser patable too.
and after creating the file it gives the save as diaglog box so that i can save the file that is created.
any other logic is also welle
i am doing it well in IE,
but the same code isn't running in the other browsers
can any body tell me how i create a .txt file using javascript which is browser patable too.
and after creating the file it gives the save as diaglog box so that i can save the file that is created.
any other logic is also welle
i am doing it well in IE,
but the same code isn't running in the other browsers
Share Improve this question edited Jul 28, 2011 at 5:48 Susam Pal 34.3k12 gold badges83 silver badges105 bronze badges asked Jul 28, 2011 at 5:00 adityaaditya 4954 gold badges12 silver badges22 bronze badges 1- I assume you're using an ActiveX object to create the file so far? – Jason Kaczmarsky Commented Jul 28, 2011 at 5:05
6 Answers
Reset to default 2You can't do this, for hopefully obvious security reasons. JavaScript has no access to the file system...in IE it's not JavaScript, but ActiveX doing this...it just has a JavaScript API exposed.
The problem isn't that Firefox doesn't do this...it's that IE ever allowed it :)
In this post In Firefox, Write to a File using Javascript?
If you're looking for IE only solution, try this:
function createFile() {
set fso = new ActiveXObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("C:\test.txt", True);
s.writeline("HI");
s.writeline("Bye");
s.writeline("-----------------------------");
s.Close();
}
you need to send the data to the server and then offer a link to download it. Here's a terrible example with jquery and php just to give you basic idea.
$.ajax({
type: "post",
url: "ajax.php",
data: {
type: "save",
text: "this is some text you want to send"
},
dataType: "json",
success: function(data){
window.open(data["url"]);
}
});
ajax.php
<?php
if($_POST["type"] == "save"){
$name = "random_name.txt";
file_put_contents("$name",$_POST["text"]);
echo json_encode(array(
"type" => "link",
"url" => "http://yourserver./{$name}"
));
}
?>
For a great example of how to do this, look at TiddlyWiki which implements a single user wiki in Javascript. It supports all the major browsers and in each will save a copy of itself to the local disk.
It uses the FileSystemObject in IE (as described previously in this question) The best info for file saves in FireFox is https://developer.mozilla/en/Code_snippets/File_I%2F%2FO
For Chrome, Opera & Safari is uses a little applet:
The TiddlySaver Java applet allows TiddlyWiki to save changes in a local version (from a file:// URL) of Safari, Opera and other browsers.
You can only do this by sending your data to a server-side language, which can write to files. Then you could send the location of the file back and redirect the user there.
One may indeed initiate data URL downloads, including in a way to prompt a file dialog (though not with a default path or even file type). See https://stackoverflow./a/13696029/271577 for such a solution (along with text link examples). That being said, opening the content in a new tab via data URLs may be a better solution if you can get users to manually save using their browser.
本文标签: htmljavascript code to save a txt fileStack Overflow
版权声明:本文标题:html - javascript code to save a txt file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741391886a2376148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论