使用 JavaScript 创建导出函数?

发布于 2024-10-03 02:21:41 字数 535 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(3

掀纱窥君容 2024-10-10 02:21:41

您是否尝试过类似的操作:

window.open("data:text/plain;charset=utf-8," + localStorage.WebAppData);

对于下载,我想您需要往返服务器,这将设置一个哑剧/类型,使下载框弹出。

编辑
如果您使用 localStorage,可能是 window.postMessage< /code>在您的环境中可用,有助于提高速度。

Did you try something like:

window.open("data:text/plain;charset=utf-8," + localStorage.WebAppData);

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 be window.postMessage is available in your environment and could help for speed.

紫罗兰の梦幻 2024-10-10 02:21:41

为了保留使用 window.open 导出的数据中的换行符,您可以使用encodeURI 包装数据:

var data1 = "Line \n break. And \r\n another one";
window.open("data:application/octet-stream, " + encodeURI(data1));

否则,您可以使用 btoa 函数导出以 base64 编码的数据:

var data1 = "Line \n break. And \r\n another one";
window.open("data:application/octet-stream;base64, " + btoa(data1));

In order to retain line-breaks in the data exported with window.open you may wrap up your data with encodeURI:

var data1 = "Line \n break. And \r\n another one";
window.open("data:application/octet-stream, " + encodeURI(data1));

Otherwise you may export your data encoded in base64 with the btoa function:

var data1 = "Line \n break. And \r\n another one";
window.open("data:application/octet-stream;base64, " + btoa(data1));
萌酱 2024-10-10 02:21:41

并不是真正的解决方案,而是一种解决方法,但是您的问题和 @Mic 的答案引导我走这条路线:

只需使用 data:text/html ,然后您可以使用 < 来插入换行符code>

  1. 我尝试了其他所有方法(unicode 字符的所有组合等)来在 text/plain 中获取换行符,但无法让它们显示出来。 document.write()document.body.textContent() 等也遇到同样的问题。换行符会被忽略。
  2. 由于 Chrome 无论如何都不允许您保存弹出窗口,因此从中获取文本的唯一方法是复制和粘贴,因此使用 text/plain 比使用 text/html 没有任何好处
  3. 在允许您保存页面的网络浏览器 (Firefox) 中,您可以选择将页面保存为文本,而不是 HTML,因此您仍然会得到相同的最终结果。

编辑:这种方法适用于 Chrome,但不适用于 Firefox,

win = window.open("", "win")
win.document.body.innerText = "Line \n breaks?"

但必须使用 innerTextInnerHTMLtextContent 删除换行符。这对两者都适用:

win = window.open("", "win")
win.document.body.innerHTML = "<pre>Line \n breaks?</pre>"

所以也许您可以将所有内容都包装在

 标记中?尽管我猜想这两者都与 `
建议具有相同的“问题”,因为它实际上创建的是 HTML 文档而不是文本/纯文本文档。

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 />

  1. I tried everything else (all combinations of unicode characters, etc, ) to get line breaks in text/plain but couldn't get them to show up. document.write() and document.body.textContent(), etc also suffer from the same problem. Line breaks just get ignored.
  2. Since Chrome won't let you save the popup window anyway, the only way to get text out of it is copy and paste so there is no benefit of using text/plain over text/html
  3. In web browsers that will let you save the page (Firefox) you can choose to save the page as text, rather than HTML and so you still get the same end result.

EDIT: This approach works in Chrome, but not Firefox

win = window.open("", "win")
win.document.body.innerText = "Line \n breaks?"

Have to use innerText though. InnerHTML or textContent remove the line breaks. This works on both:

win = window.open("", "win")
win.document.body.innerHTML = "<pre>Line \n breaks?</pre>"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文