简体   繁体   English

如何提示用户将文本(xml)保存到文件?

[英]How do I prompt user to save text (xml) to a file?

I've have some javascript code that transforms XML with XSLT. 我有一些JavaScript代码可以使用XSLT转换XML。 I now want the user to be able to save that new XML (either by prompting them or throwing the new XML up as a file or something so that the user can then save it. Anybody know how to do that? 我现在希望用户能够保存该新XML(通过提示它们或将新XML作为文件或其他东西抛出,以便用户随后可以将其保存。有人知道该怎么做吗?

var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = false;
xmldoc.loadXML(responseText); // responseText is xml returned from ajax call
//apply the xslt
var xsldoc = new ActiveXObject("Microsoft.XMLDOM");
xsldoc.async = false;
xsldoc.load("../xslt/ExtraWorkRequest.xslt");
var content = xmldoc.transformNode(xsldoc);

How do I get the user to save the XML (content) as a file? 如何让用户将XML(内容)另存为文件?

By default, you can't. 默认情况下,您不能这样做。 Browser isn't supposed to access your local disks for security reasons. 出于安全原因,浏览器不应访问您的本地磁盘。

But, if you can ask your user to change it's security settings (and you should not to ask), you can to use FileSystemObject or even your Microsoft.XMLDOM.Save method. 但是, 如果您可以要求用户更改其安全设置 (并且您不应该要求),则可以使用FileSystemObject甚至Microsoft.XMLDOM.Save方法。

You cannot do it with 100% client-side JavaScript with the default security settings. 您不能使用具有默认安全设置的100%客户端JavaScript来执行此操作。 You'll need to implement some server-side logic. 您将需要实现一些服务器端逻辑。 In your case, you'll be able to do the XML transformation server-side, as well. 就您而言,您也可以在服务器端进行XML转换。

http://www.bigresource.com/Tracker/Track-javascripts-ijfTJlI9/ http://www.bigresource.com/Tracker/Track-javascripts-ijfTJlI9/

http://support.microsoft.com/kb/q260519/ http://support.microsoft.com/kb/q260519/

You could construct a data: URI with a media type of application/octet-stream . 您可以构造一个数据:URI,其媒体类型为application/octet-stream

function download (data, charset) {
  if (!charset) {
    charset = document.characterSet;
  }
  location.href = ["data:application/octet-stream;charset=",
                   charset, ",", encodeURIComponent(data)
                  ].join("");
}

All browsers except IE support data: URIs. 除IE之外的所有浏览器均支持数据:URI。 I think IE8 may support them, but only for images. 我认为IE8可能支持它们,但仅适用于图像。 For IE, a workaround could be to send the data to a server (including document.characterSet ) and then load a page that has something like the following header: 对于IE,一种解决方法是将数据发送到服务器(包括document.characterSet ),然后加载具有以下标头的页面:

Content-Type: application/xml; charset={document.characterSet}
Content-Disposition: attachment

If you want to give the file a name too, use Content-Disposition: attachment; filename=... 如果您也想给文件命名,请使用Content-Disposition: attachment; filename=... Content-Disposition: attachment; filename=... . Content-Disposition: attachment; filename=...

Also, for any of this to work, you have to convert your XML to a string first. 另外,为了使这些方法都能起作用,您必须首先将XML转换为字符串。

I do this with code snippets on my blog (user can click on the save button, and the snippet will come up in their default text editor, where they can tweak it and/or copy it into their app). 我使用博客上的代码片段来执行此操作(用户可以单击“保存”按钮,并且该片段将出现在其默认文本编辑器中,用户可以在其中进行调整和/或将其复制到自己的应用中)。

It works by putting all the textual data inside of a hidden field, and then submits it to a very simple server-side HTTP handler. 通过将所有文本数据放在一个隐藏字段中,然后将其提交到一个非常简单的服务器端HTTP处理程序,它可以工作。 The handler just grabs the hidden field value and spits it right back out in the response with the right content-disposition header, giving the user the open/save download prompt. 处理程序只是获取隐藏的字段值,然后使用正确的content-disposition标头将其直接返回到响应中,从而向用户提供打开/保存下载提示。

This is the only way I could get it to work. 这是我可以使用它的唯一方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM