如何动态地创建用于下载的Javascript文件? [英] How do I dynamically create a document for download in Javascript?

查看:208
本文介绍了如何动态地创建用于下载的Javascript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一些JavaScript code,在客户端生成一个XML文档(通过的谷歌地球插件)。我希望用户能够点击页面上的按钮,提示将XML保存到一个新文件。如果我生成XML服务器端,这将是很容易,只要让按钮打开该链接。但XML生成的客户端。

我已经拿出了几个黑客的一半工作,通过<一的启发href=\"http://stackoverflow.com/questions/904893/saving-a-child-window-saves-the-parent-window-instead-javascript\">this StackOverflow的问题。但既不是完全正常工作。这里是嵌入code演示HTML:

 &LT; HTML和GT;&LT; HEAD&GT;&LT;脚本&GT;
函数的getData(){返回'&LT; XML版本=1.0编码=UTF-8&GT;&LT;&DOC GT;您好&LT; / DOC&GT;'; }
功能dlDataURI(){
  window.open(数据:文本/ XML的,字符集= UTF-8,+的getData());
}
功能dlWindow(){
  变种W = window.open();
  w.document.open();
  w.document.write(的getData());
  w.document.close();
}
&LT; / SCRIPT&GT;&LT;身体GT;
&LT; D​​IV的onclick =dlDataURI()&gt;点击数据网址&LT; / DIV&GT;
&LT; D​​IV的onclick =dlWindow()&gt;点击为窗口和LT; / DIV&GT;
&LT; /身体GT;&LT; / HTML&GT;

该dlDataURI()版本的Firefox在Chrome的伟大工程,质量低劣,(不能保存),而不是在所有在IE中。窗口()版本,Firefox和IE浏览器的工作原理确定,并没有得到很好的在Chrome中(不能保存,嵌入到HTML,XML)。无论是以往的版本提示用户下载,它总是会打开一个新的窗口,试图显示XML。

有没有办法做我想做的在客户端的JavaScript的好方法?我想这在当今的浏览器,火狐理想,MSIE 8和Chrome工作。

更新样本Downloadify code

 的window.onload =函数(){
  Downloadify.create(dlify,{
    数据:的getData(),
    文件名:data.xml中
    SWF:媒体/ downloadify.swf',
    downloadImage:图像/ download.png',
    宽度:100,高度:30});};


解决方案

据我见过的最好的是 Downloadify 通过道格Neiner ,它需要Flash,但工作得很好:


  

一个微小的JavaScript + Flash库,使动态文件的生成和保存,在浏览器中,无需服务器交互。


勾选此影片

I'm writing some Javascript code that generates an XML document in the client (via Google Earth plugin). I'd like the user to be able to click a button on the page and be prompted to save that XML to a new file. If I were generating the XML server-side this would be easy, just make the button open the link. But the XML is generated client-side.

I've come up with a couple of hacks that half-work, inspired in part by this StackOverflow question. But neither completely work. Here's a demo HTML with embedded code:

<html><head><script>
function getData() { return '<?xml version="1.0" encoding="UTF-8"?><doc>Hello</doc>'; }
function dlDataURI() {
  window.open("data:text/xml;charset=utf-8," + getData());
}
function dlWindow() {
  var w = window.open();
  w.document.open();
  w.document.write(getData());
  w.document.close();
}
</script><body>
<div onclick="dlDataURI()">Click for Data URL</div>
<div onclick="dlWindow()">Click for Window</div>
</body></html>

The dlDataURI() version works great in Firefox, poorly in Chrome (can't save), and not at all in IE. The Window() version works OK in Firefox and IE, and not well in Chrome (can't save, XML embedded inside HTML). Neither version ever prompts a user download, it always opens a new window trying to display the XML.

Is there a good way to do what I want in client side Javascript? I'd like this to work in today's browsers, ideally Firefox, MSIE 8, and Chrome.

Update with sample Downloadify code

window.onload = function() {
  Downloadify.create("dlify", {
    data: getData(),
    filename: "data.xml",
    swf: 'media/downloadify.swf',
    downloadImage: 'images/download.png',
    width: 100, height: 30});};

解决方案

The best I've seen as far is Downloadify by Doug Neiner, it requires Flash but works very well:

"A tiny JavaScript + Flash library that enables the generation and saving of files on the fly, in the browser, without server interaction."

Check this video.

这篇关于如何动态地创建用于下载的Javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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