javascript 用于保存txt文件的javascript代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6854296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:06:45  来源:igfitidea点击:

javascript code to save a txt file

javascripthtml

提问by aditya

can any body tell me how i create a .txt file using javascript which is browser compatable too.

任何人都可以告诉我如何使用浏览器兼容的 javascript 创建 .txt 文件。

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 wellcome

任何其他逻辑也是受欢迎的

i am doing it well in IE,

我在 IE 中做得很好,

but the same code isn't running in the other browsers

但相同的代码没有在其他浏览器中运行

回答by Mrchief

If you're looking for IE only solution, try this:

如果您正在寻找仅限 IE 的解决方案,请尝试以下操作:

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();
}

回答by hirikarate

You 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 :)

出于明显的安全原因,您不能这样做。JavaScript 无法访问文件系统......在 IE 中它不是 JavaScript,而是 ActiveX 这样做......它只是公开了一个 JavaScript API。

问题不在于 Firefox 不这样做……而是 IE 曾经允许这样做:)

In this post In Firefox, Write to a File using Javascript?

在这篇文章中,在 Firefox 中,使用 Javascript 写入文件?

回答by Ilia Choly

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.

您需要将数据发送到服务器,然后提供下载链接。这是一个 jquery 和 php 的可怕例子,只是为了给你基本的想法。

$.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

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.com/{$name}"
        ));
    }

?>

回答by Lindsay Morsillo

For a great example of how to do this, look at TiddlyWikiwhich 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.

有关如何执行此操作的一个很好的示例,请查看TiddlyWiki,它在 Javascript 中实现了单用户 wiki。它支持所有主要浏览器,并且在每个浏览器中都会将其自身的副本保存到本地磁盘。

It uses the FileSystemObject in IE (as described previously in this question) The best info for file saves in FireFox is https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

它在 IE 中使用 FileSystemObject(如本问题前面所述)FireFox 中文件保存的最佳信息是https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

For Chrome, Opera & Safari is uses a little applet:

对于 Chrome,Opera 和 Safari 使用了一个小程序:

The TiddlySaver Java applet allows TiddlyWiki to save changes in a local version (from a file:// URL) of Safari, Opera and other browsers.

TiddlySaver Java 小程序允许 TiddlyWiki 在 Safari、Opera 和其他浏览器的本地版本(来自 file:// URL)中保存更改。

回答by Brett Zamir

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.com/a/13696029/271577for 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.

确实可以启动数据 URL 下载,包括以提示文件对话框的方式(尽管没有默认路径甚至文件类型)。有关此类解决方案(以及文本链接示例),请参阅https://stackoverflow.com/a/13696029/271577。话虽如此,如果您可以让用户使用他们的浏览器手动保存,那么通过数据 URL 在新选项卡中打开内容可能是更好的解决方案。

回答by Jason Kaczmarsky

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.

您只能通过将数据发送到可以写入文件的服务器端语言来做到这一点。然后您可以将文件的位置发回并将用户重定向到那里。