如何下载 XML,而无需浏览器在另一个选项卡中打开它

发布于 2024-09-26 06:29:03 字数 176 浏览 3 评论 0原文

我有一个 xml 文件需要下载。 众所周知,当我提供以下链接时,

<a href="some.xml">Downlad XML</a>

XML 将在显示它的新选项卡中打开。 但是我想知道是否有办法可以像其他文件(例如 .zip 文件)一样下载它

I have an xml file to be downloaded.
As you all know when i give the below link

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

围归者 2024-10-03 06:29:03

在尝试了很多解决方案之后,这对我有用。

尝试为文件链接提供一个虚拟参数,如下所示。

<a href="http://link/to/the/file.xml?dummy=dummy" download>Download Now</a>

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>
眼睛会笑 2024-10-03 06:29:03

有一个名为 Content-Disposition 的 HTTP 标头,它在 RFC1806 中定义如下:

2.1 内联处置类型

如果想要将正文部分标记为内联
显示时自动显示
的消息。内联正文部分应按以下顺序呈现
它们的发生,受
多部分消息的正常语义。

2.2 附件处理类型

可以指定身体部位
attachment 表明它们是
与主体分开
邮件消息,并且它们的显示不应该是自动的,但是
视采取进一步行动而定
用户的。 MUA 可能会取而代之
向用户呈现位图
具有标志性表示的终端
的附件,或者,
字符终端,带有列表
用户从中获取的附件
可以选择查看或存储。

为了将标头消息放在 xml 文件中,您需要访问服务器端。例如,使用 php 的 header 函数,您可以编写如下内容:

header('Content-Disposition: attachment; filename="some.xml"');

如果您不这样做可以访问服务器端,你可以尝试以下我在谷歌上找到的 JavaScript 技巧(不确定它是否有效):

<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'some.xml');">Save this page</a> 

There's an HTTP header called Content-Disposition, which is defined in RFC1806 as follows:

2.1 The Inline Disposition Type

A bodypart should be marked inline if 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
attachment to 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.

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 header function you could write something like:

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):

<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'some.xml');">Save this page</a> 
九命猫 2024-10-03 06:29:03

我发现只需右键单击网络浏览器并选择“将页面另存为...”即可完成工作。

I found that just right clicking on the web browser and selecting "Save Page As..." does the work.

故事还在继续 2024-10-03 06:29:03

与上面 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>

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.

<a href="http://link/to/the/file.xml" download="downloadedfilename.xml">XML Download Text</a>
昔日梦未散 2024-10-03 06:29:03

使用类似的东西:

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;
    }

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;
    }
近箐 2024-10-03 06:29:03

这是我的 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

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:

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
最舍不得你 2024-10-03 06:29:03

只需在锚标记中添加“下载”属性即可

<a href="some.xml" download >Downlad XML</a>

Just add the 'download' attribute in the anchor tag

<a href="some.xml" download >Downlad XML</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文