跨浏览器另存为.txt [英] Cross-browser Save As .txt
问题描述
是否有一个JavaScript库允许将字符串保存为txt文件,并且可以跨浏览器工作?
Is there a JavaScript library that allows to save strings as txt files, and works cross-browser?
过去,我一直在使用Downloadify,但是我我正在寻找另一种选择,原因有两个:
In the past, I have been using Downloadify, but I am looking at another option for a couple reasons:
- 我希望找到一个纯粹的JavaScript解决方案,而不需要Flash
- 似乎Downloadify不再更新
(过去18个月内没有更新) - 我在IE 9中遇到Downloadify问题,字符串被切断的地方
推荐答案
据我所知,唯一的方法是使用数据:强制下载的网址:
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);
这里有两次捕获:
- 它在MSIE中不起作用,因为它对数据的支持:URL非常有限(据说是出于安全原因)。所以你仍然需要在那里下载。
- 你不能指定文件名,建议的文件名将取决于所使用的浏览器。并且文件类型将是未知(您不能使用已知的MIME类型,因为浏览器不会提供下载文件)。
- 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).
奖金阅读:2010年2月有一个关于解决第二个问题的W3.org讨论: http://lists.w3.org/Archives/Public/uri/2010Feb/thread.html#msg58 。但是,到目前为止,这似乎没有进入任何规范,更不用说浏览器实现了。
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.
这篇关于跨浏览器另存为.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!