[英]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下载。
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.