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 ther开发者_如何转开发e is a way, this can downloaded like other files such as a .zip file
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>
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>
I found that just right clicking on the web browser and selecting "Save Page As..." does the work.
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>
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;
}
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
Just add the 'download' attribute in the anchor tag
<a href="some.xml" download >Downlad XML</a>
精彩评论