怎样下载的JavaScript字符串的文件 [英] How do I download JavaScript string as a file

查看:157
本文介绍了怎样下载的JavaScript字符串的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序请求KML数据通过AJAX从服务器。此数据存储在JavaScript变量,并在谷歌地球插件显示。

Application requests KML data through AJAX from server. This data is stored in javascript variables, and displayed in Google Earth plugin.

在JavaScript中,我如何提供一个链接到下载存储在JavaScript变量的KML数据(字符串),而不需要一个请求到服务器?

In javascript, how do I provide a link to download the KML data stored in javascript variable (as a string) without requiring a request back to server?

此链接: http://forum.mootools.net/viewtopic.php?id=9728

建议使用数据URI的,但可能不会跨足够的浏览器为我的需求。也许最简单的只是回到服务器重新获取数据的下载,但好奇,如果有人曾与JavaScript的拉这一关。

suggests the use of data URI, but that probably won't work across enough browsers for my needs. Probably simplest just to go back to server to get data again for download, but curious if anyone has pulled this off with javascript.

推荐答案

简短的回答:你不能,仍然是独立的平台。大多数浏览器就是不支持Java脚本来操作文件系统。

Short answer: you can't and still be platform independent. Most browsers just don't allow javascript to manipulate the filesystem.

这是说,你也许能逃脱一些非常特定于平台的黑客。例如,IE浏览器提供ExecCommand函数,你可以用它来打电话另存为。如果你这样做的IFrame是有你想保存的数据中,你可能会得到它的工作 - 但只有在IE浏览器。其他选项(同样,我要去微软的具体位置)包含的这个Silverlight的破解或ActiveX控件。

That said, you might be able to get away with some very platform-specific hacks. For example, IE offers the execCommand function, which you could use to call SaveAs. If you did that within an IFrame that had the data you wanted to save, you might get it working -- but only on IE. Other options (again, I'm going Microsoft specific here) include this Silverlight hack, or ActiveX controls.

我觉得对于全平台兼容,你只是将不得不吮吸它,并提供了一​​个服务器端的下载选项。

I think for full platform compatibility, you're just going to have to suck it up and provide a server-side download option.

哎呦!我没有做足够的尽职调查,当我去链接狩猎。原来,Silverlight的黑客我联系到了一个服务器端组件。看起来你是pretty的SOL。

Whoops! I didn't do enough due diligence when I went link-hunting. It turns out the Silverlight hack I linked to has a server-side component. Looks like you're pretty SOL.

我发现浏览器兼容性的将execCommand 一个很好的总结这里。虽然列出了问号的另存为命令,也许这可能是你一个很好的途径毕竟。值得一试,也许?

I found a nice summary of browser compatibility for execCommand here. Although it lists question marks for the "saveas" command, maybe that might be a good route for you after all. Worth a try, perhaps?

好吧,我决定做我建议的方法的概念验证,我得到的东西很简单,即工作。不幸的是,我证明了在这个过程中,这种做法用于Firefox 将无法正常工作,并没有按'T出现在Chrome / Safari浏览器的工作,无论是。所以这是非常依赖于平台。但它的作品!下面是一个完整的工作页面:

Well, I decided to do a proof of concept of the approach I suggested, and I got something fairly simple working in IE. Unfortunately, I proved in the process that this approach will not work for Firefox and doesn't appear to work in Chrome/Safari, either. So it's very platform dependent. But it works! Here's a complete working page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Javascript File Saver</title>
    <script type="text/javascript">
      function PageLoad() {
        var fdoc = window.frames["Frame"].document;
        fdoc.body.appendChild(fdoc.createTextNode("foo,bar,baz"));
      }
      function Save() {
        var fdoc = window.frames["Frame"].document;
        fdoc.execCommand("SaveAs", true);
      }
    </script>
</head>
<body onload="PageLoad();">
<h2>Javascript File Saver</h2>
<iframe id="Frame" style="width: 400px;">Noframe</iframe><br />
<button onclick="Save();">Save</button>
</body>
</html>

这篇关于怎样下载的JavaScript字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆