如何在没有浏览器在另一个选项卡中打开它的情况下下载 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3860750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download an XML without the browser opening it in another tab
提问by Alloi
I have an xml file to be downloaded. As you all know when i give the below link
我有一个 xml 文件要下载。众所周知,当我提供以下链接时
<a href="some.xml">Downlad XML</a>
The XML will open in a new tab displaying it. However I would like to know if there is a way, this can downloaded like other files such as a .zip file
XML 将在显示它的新选项卡中打开。但是我想知道是否有办法,这可以像其他文件一样下载,例如 .zip 文件
回答by Eugene Yokota
There's an HTTP header called Content-Disposition, which is defined in RFC1806as follows:
有一个名为 Content-Disposition 的 HTTP 标头,它在RFC1806 中定义如下:
2.1 The Inline Disposition Type
A bodypart should be marked
inlineif it is intended to be displayed automatically upon display of the message. Inline bodyparts should be presented in the order in which they occur, subject to the normal semantics of multipart messages.2.2 The Attachment Disposition Type
Bodyparts can be designated
attachmentto indicate that they are separate from the main body of the mail message, and that their display should not be automatic, but contingent upon some further action of the user. The MUA might instead present the user of a bitmap terminal with an iconic representation of the attachments, or, on character terminals, with a list of attachments from which the user could select for viewing or storage.
2.1 内联处置类型
inline如果要在显示消息时自动显示正文部分,则应标记正文部分。内联正文部分应按它们出现的顺序呈现,受多部分消息的正常语义约束。2.2 附件处置类型
可以指定正文部分
attachment以指示它们与邮件消息的主体分开,并且它们的显示不应是自动的,而是取决于用户的某些进一步操作。MUA 可能会向位图终端的用户提供附件的图标表示,或者在字符终端上,向用户提供附件列表,用户可以从中选择查看或存储。
In order to put the header message on the xml file, you'd need the access to the server-side. For example using php's headerfunction you could write something like:
为了将标题消息放在 xml 文件上,您需要访问服务器端。例如,使用 php 的header函数,您可以编写如下内容:
header('Content-Disposition: attachment; filename="some.xml"');
If you don't have access to the server-side, you could try the following JavaScript trick that I found Googling (not sure if it would work):
如果您无权访问服务器端,您可以尝试以下我在谷歌搜索中发现的 JavaScript 技巧(不确定它是否有效):
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'some.xml');">Save this page</a>
回答by Mohd Abdul Mujib
After trying a lot of solutions, This worked for me.
在尝试了很多解决方案之后,这对我有用。
Try to provide a dummy argument to the file link as shown below.
尝试为文件链接提供一个虚拟参数,如下所示。
<a href="http://link/to/the/file.xml?dummy=dummy" download>Download Now</a>
回答by evox
I found that just right clicking on the web browser and selecting "Save Page As..." does the work.
我发现只需右键单击 Web 浏览器并选择“将页面另存为...”即可。
回答by mwopata
Similar to Mohd's solution above, I was able to use the download attribute as specified by W3 here: http://www.w3schools.com/tags/att_a_download.asp. A cool trick about going this route is that you can specify what you want the downloaded filename to be, if it is different from the filename on the server.
与上面 Mohd 的解决方案类似,我可以使用 W3 在此处指定的下载属性:http: //www.w3schools.com/tags/att_a_download.asp。走这条路线的一个很酷的技巧是,如果下载的文件名与服务器上的文件名不同,您可以指定希望下载的文件名。
<a href="http://link/to/the/file.xml" download="downloadedfilename.xml">XML Download Text</a>
回答by Ryszard J?draszyk
Here is my VBA solution. The macro will download the XML file, save it in macro file's folder and inform the user once the process has been completed:
这是我的 VBA 解决方案。宏将下载 XML 文件,将其保存在宏文件的文件夹中,并在该过程完成后通知用户:
Sub SaveXMLFromWeb()
Dim XDoc As Object
Dim XMLpath As String, outPath As String
XMLpath = "http://example.com/examplefile.xml" 'change to your URL
outPath = ThisWorkbook.Path & "\yourFileName.xml" 'change output file name
Set XDoc = CreateObject("MSXML2.DOMDocument")
With XDoc
.async = False
.validateOnParse = False
.Load (XMLpath)
.Save (outPath)
End With
MsgBox ("XML file has been downloaded and saved in the following location:" & String(2, vbLf) & outPath)
End Sub
回答by Orod Semsarzadeh
use something like:
使用类似的东西:
private function sendXml($xml, $label) {
ob_clean();
header('Content-type: text/plain; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $label . '.xml"');
echo ltrim($xml);
ob_flush();
exit;
}

