跨浏览器另存为.txt

发布于 2024-11-16 17:50:31 字数 247 浏览 2 评论 0原文

是否有一个 JavaScript 库允许将字符串保存为 txt 文件,并且可以跨浏览器工作?

过去,我一直在使用 Downloadify,但出于以下几个原因我正在考虑另一个选择:

  • 我希望找到一个纯 JavaScript 解决方案,不需要 Flash
  • 似乎 Downloadify 不再更新 (过去 18 个月没有更新)
  • 我在 IE 9 中遇到 Downloadify 问题,字符串被切断

Is there a JavaScript library that allows to save strings as txt files, and works cross-browser?

In the past, I have been using Downloadify, but I am looking at another option for a couple reasons:

  • I hope to find a pure JavaScript solution, without the need for Flash
  • it seems that Downloadify is not updated anymore
    (no update in the past 18 months)
  • I am experiencing an issue with Downloadify in IE 9, where strings are cut off

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-11-23 17:50:31

这是您需要的。但它还不是跨浏览器的。适用于 Google Chrome。

<a download="MyFile.txt" 
   href="your-data-uri-here"
   draggable="true" 
   class="dragout"
>Download ready</a>

维基百科还有一篇很好的关于数据 URI 的文章

Here is what you need. But it's not cross-browser yet. Works in Google Chrome.

<a download="MyFile.txt" 
   href="your-data-uri-here"
   draggable="true" 
   class="dragout"
>Download ready</a>

Also Wikipedia has a good article about Data URI

拥抱影子 2024-11-23 17:50:31

据我所知,唯一的方法是使用 data: URL 强制下载:

var data = "This is a test";
window.location.href = "data:application/x-download;charset=utf-8," + encodeURIComponent(data);

这里有两个问题:

  • 它在 MSIE 中不起作用,因为它对 data: URL 的支持非常有限(据说 出于安全原因)。所以你仍然需要 Downloadify 。
  • 您无法指定文件名,建议的文件名将取决于所使用的浏览器。并且文件类型将是“未知”(您不能使用已知的 MIME 类型,因为浏览器将不会提供下载该文件的功能)。

额外阅读:2010 年 2 月,W3.org 进行了一次有关解决第二个问题的讨论:http://lists.w3.org/Archives/Public/uri/2010Feb/thread.html#msg58。然而,到目前为止,这似乎还没有纳入任何规范,更不用说浏览器实现了。

As far as I know, the only way is to use data: URLs to force a download:

var data = "This is a test";
window.location.href = "data:application/x-download;charset=utf-8," + encodeURIComponent(data);

Two catches here:

  • It won't work in MSIE because its support of data: URLs is very limited (supposedly for security reasons). So you will still need Downloadify there.
  • You cannot specify a file name, the suggested file name will depend on the browser used. And file type will be "unknown" (you cannot use a known MIME type because the browser won't offer to download the file then).

Bonus reading: there was a W3.org discussion in February 2010 on fixing the second problem: http://lists.w3.org/Archives/Public/uri/2010Feb/thread.html#msg58. However, this doesn't seem to have made it into any specification so far, let alone browser implementations.

小糖芽 2024-11-23 17:50:31

FileSaver API 跨浏览器兼容

var text = "Hello, world!";
var blob = new Blob([text], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "filename.txt");

FileSaver API is cross-browser compatible

var text = "Hello, world!";
var blob = new Blob([text], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "filename.txt");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文