简体   繁体   English

如何生成xml文件并允许用户使用javascript将其保存在本地?

[英]how do I generate an xml file and allow the user to save it locally using javascript?

I am generating a XMl document via JavaScript and need to prompt the user to save it to their local machine. 我正在通过JavaScript生成XMl文档,需要提示用户将其保存到本地计算机上。

This is the code I have at the moment: 这是我目前的代码:

var xmlWindow = window.open("", "");
xmlWindow.document.write("<node>data</node>");

This writes the xml to a new window which the user can then hit file->save to download. 这会将xml写入新窗口,然后用户可以单击file-> save下载。

  1. Is there any more elegent way of doing this? 还有其他更优雅的方式吗?
  2. Is there any way to set the mimeType of the new window so the xml displays properly? 有什么方法可以设置新窗口的mimeType,以便xml正确显示吗?
  3. is there any way to specify the title so when the user hits save, the file name is set correctly? 有什么方法可以指定标题,以便在用户单击保存时正确设置文件名?

note: needs to be compatible with most brosers (IE6, IE7, FF, Chrome). 注意:需要与大多数浏览器(IE6,IE7,FF,Chrome)兼容。

You could send the XML through an XMLHttpRequest to a script on your server to make the xml file, then set window.location to the location of your new xml file. 您可以通过XMLHttpRequest将XML发送到服务器上的脚本以创建xml文件,然后将window.location设置为新xml文件的位置。 They'll be prompted to save it. 将提示他们保存它。

"save it locally": A tough call, if you want to be compatible across browsers. “将其保存在本地”:如果您希望跨浏览器兼容,这是一个艰难的选择。 See this post . 看到这篇文章

TiddlyWiki manages to do this for some browsers (not sure which ones it supports) but only by having the user install certain files. TiddlyWiki设法对某些浏览器(不确定其支持的浏览器)执行此操作,但只能通过让用户安装某些文件来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何提示用户将文本(xml)保存到文件? - How do I prompt user to save text (xml) to a file? 在javascript中生成一些xml,提示用户保存它 - Generate some xml in javascript, prompt user to save it 我如何允许用户将文件上传到我的网站,对其进行编辑并将其保存回去 - How do I allow the user to upload file to my site, edit it and save it back 如何使用高效的 Javascript 在本地存储多个用户更改? - How do I store multiple user changes locally with efficient Javascript? 如何使用 JavaScript 将 Web API 保存到 JSON 文件? - How do I save a web API to JSON file using JavaScript? Javascript生成文件并要求用户保存它,可能吗? - Javascript generate file and ask user to save it, possible? 如何在本地保存javascript变量? - How can I save javascript variables locally? 如何在用户离线时将 xml 文件保存到本地存储并更新值? - How do I save xml file to local storage and update values when user is offline? 如何在 javascript 中使用正则表达式在用户表单输入中只允许一个(破折号或点或下划线)? - How do i allow only one (dash or dot or underscore) in a user form input using regular expression in javascript? 如何让用户保存文件并仅在浏览器 Javascript 中继续编辑该文件? - How do I let user save a file and keep editing that file in browser Javascript only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM