21

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

2
  • Need to be done on the server side. Do you have access to the server side code, and if so is it PHP or what?
    – sje397
    Commented Oct 5, 2010 at 4:10
  • 1
    Are you the client or the server? Your question is quite ambiguous.
    – msw
    Commented Oct 5, 2010 at 4:59

7 Answers 7

18

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>
3
16

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> 
2
  • 1
    Is this supported by all browsers?
    – sje397
    Commented Oct 5, 2010 at 4:12
  • I can't guarantee all browsers, but it should work on most browsers if they respect HTTP. Commented Oct 5, 2010 at 4:15
7

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

1
  • 1
    OK - this needs to be upvoted about 80 times!
    – Destek
    Commented Oct 22, 2021 at 18:41
1

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>
0
0

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;
    }
1
  • 1
    If you post code or XML, please highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it!
    – marc_s
    Commented Oct 5, 2010 at 6:24
0

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
0

Just add the 'download' attribute in the anchor tag

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.