在javascript中生成一些xml,提示用户保存

2022-01-24 00:00:00 save javascript

我想在 JavaScript 中创建一个 XML 文档,然后出现一个保存对话框.

I'd like to make an XML document in JavaScript then have a save dialog appear.

  1. 如果他们必须点击才能进行保存,那也没关系.
  2. 如果我*必须*使用 IE 来实现这一点,那*不*可以(我什至根本不需要支持它).但是,Windows 是必需的平台(如果我只能在一个浏览器中执行此操作,那么 Firefox 或 Chrome 是首选浏览器).
  3. 如果我需要 Web 服务器,那*不* 可以.但相反,我不想要求 JavaScript 仅在本地文件上运行,即提升权限 - 如果可能的话.也就是说,我想在本地或 *static* 主机上运行.但就在本地就可以了.
  4. 必须向后弯腰才能做到这一点.该文件不会很大,但互联网访问可能存在、参差不齐或根本不可能 - 请参阅 (3).

到目前为止,我看到的唯一想法是将 XML 保存到 iframe 并保存该文档——但您似乎只能在 IE 中执行此操作?另外,我可以构造一个数据 URI 并将其放在一个链接中.我担心的是它只会在窗口中打开 XML 文件,而不是提示用户保存它.

So far the only ideas I have seen are to save the XML to an iframe and save that document -- but it seems that you can only do this in IE? Also, that I could construct a data URI and place that in a link. My fear here is that it will just open the XML file in the window, rather than prompt the user to save it.

我知道如果我要求 JavaScript 是本地的,我可以提高权限并直接保存文件(或者希望出现一个保存对话框).但是,我更喜欢不需要提升权限的解决方案(即使是仅 Firefox 3.6 的解决方案).

I know that if I require the JavaScript to be local, I can raise privileges and just directly save the file (or hopefully cause a save dialog box to appear). However, I'd much prefer a solution where I do not require raised privileges (even a Firefox 3.6 only solution).

如果这冒犯了任何人的感受(例如,不支持所有浏览器),我深表歉意.我基本上想编写一个离线应用程序,考虑到需求的复杂性和可用的时间,Javascript/HTML/CSS 似乎是最好的候选人.但是,我有一个要求是能够保存数据,在我选择这条开发线之前必须克服这个要求.

I apologize if this offends anyone's sensibilities (for example, not supporting every browser). I basically want to write an offline application and Javascript/HTML/CSS seem to be the best candidate considering the complexity of the requirements and the time available. However, I have this single requirement of being able to save data that must be overcome before I can choose this line of development.

推荐答案

这个如何downloadify脚本?

这是基于Flash和jQuery的,它可以提示您将文件保存在您的计算机中的对话框.

Which is based on Flash and jQuery, which can prompt you dialog box to save file in your computer.

Downloadify.create('downloadify',{
  filename: function(){
    return document.getElementById('filename').value;
  },
  data: function(){ 
    return document.getElementById('data').value;
  },
  onComplete: function(){ 
    alert('Your File Has Been Saved!'); 
  },
  onCancel: function(){ 
    alert('You have cancelled the saving of this file.');
  },
  onError: function(){ 
    alert('You must put something in the File Contents or there will be nothing to save!'); 
  },
  swf: 'media/downloadify.swf',
  downloadImage: 'images/download.png',
  width: 100,
  height: 30,
  transparent: true,
  append: false
});

相关文章