使用 JavaScript 创建导出函数?
我正在尝试在 JavaScript 中为打包的 Web 应用程序设置一个导出函数,该函数将存储在 localStorage 中的字符串转换为纯文本文件以供下载。由于 JavaScript 无法访问计算机的文件系统,我想对其进行设置,以便它创建一个空白文本文件(或者,如果失败,则创建一个简单的 HTML 页面)并在网络浏览器中打开;因为它不会访问任何文件系统,所以我希望这是可能的。
我正在考虑使用数据 URI 方案以纯文本形式打开 localStorage,如下所示:
function exportFile() {
window.open("data:text/plain;charset=utf-8," + localStorage.WebAppData);
};
但它比我预期的要慢得多,我猜这是因为它将整个文档粘贴在 URL 框中。虽然可能不是代码问题,但某些网络浏览器(例如 Google Chrome)不允许我保存生成的文件。 (由于某种原因,所有换行符都变成了空格......)
任何解决这些问题的建议或执行类似功能的更好方法将不胜感激!
I'm trying to set up an export function in JavaScript for a packaged web app that turns a string stored in localStorage into a plain text file for downloading. As JavaScript does not have access to the computer's file-system, I'd like to set it up so that it create a blank text file (or, failing that, a simple HTML page) and open in in the web-browser; as it wouldn't be accessing any file-systems I was hoping this would be possible.
I was thinking of using a Data URI scheme to open the localStorage as plain text, such as the following:
function exportFile() {
window.open("data:text/plain;charset=utf-8," + localStorage.WebAppData);
};
But it's much slower than I expected, which I guess is because it's sticking the whole document in the URL box. Though probably not an issue with the code, some web browsers, like Google Chrome, won't let me save the resulting file. (And for some reason all the line-breaks have turned into spaces....)
Any suggestions to fix these problems or better ways of doing a similar function will be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过类似的操作:
对于下载,我想您需要往返服务器,这将设置一个哑剧/类型,使下载框弹出。
编辑:
如果您使用
localStorage
,可能是window.postMessage< /code>
在您的环境中可用,有助于提高速度。
Did you try something like:
For the download, I guess you need a round trip to a server, that will set a mime/type that will make the download box to pop up.
EDIT:
If you use
localStorage
, may bewindow.postMessage
is available in your environment and could help for speed.为了保留使用 window.open 导出的数据中的换行符,您可以使用encodeURI 包装数据:
否则,您可以使用 btoa 函数导出以 base64 编码的数据:
In order to retain line-breaks in the data exported with window.open you may wrap up your data with encodeURI:
Otherwise you may export your data encoded in base64 with the btoa function:
并不是真正的解决方案,而是一种解决方法,但是您的问题和 @Mic 的答案引导我走这条路线:
只需使用
data:text/html
,然后您可以使用 < 来插入换行符code>text/plain
中获取换行符,但无法让它们显示出来。document.write()
和document.body.textContent()
等也遇到同样的问题。换行符会被忽略。text/plain
比使用text/html 没有任何好处
编辑:这种方法适用于 Chrome,但不适用于 Firefox,
但必须使用
innerText
。InnerHTML
或textContent
删除换行符。这对两者都适用:所以也许您可以将所有内容都包装在
Not really a solution, rather a work-around, but your question and the answer by @Mic lead me down this route:
Just use
data:text/html
as then you can put in line breaks using<br />
text/plain
but couldn't get them to show up.document.write()
anddocument.body.textContent()
, etc also suffer from the same problem. Line breaks just get ignored.text/plain
overtext/html
EDIT: This approach works in Chrome, but not Firefox
Have to use
innerText
though.InnerHTML
ortextContent
remove the line breaks. This works on both:So perhaps you could just wrap everything in
<pre>
tags? Although I guess both of these have the same "problem" as the `suggestion in that it's actually creating a HTML document rather than a text/plain one.