20

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Alloi
  • 663
  • 2
  • 7
  • 10
  • 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 Oct 05 '10 at 04:10
  • 1
    Are you the client or the server? Your question is quite ambiguous. – msw Oct 05 '10 at 04:59

7 Answers7

16

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>
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • 1
    Thanks. The `download` attribute was all I needed, at least for Chrome 47. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a – Steve Tjoa Jan 18 '16 at 20:36
  • Caution: as of 8/22/2016, download is not supported in Safari browsers :/ See http://caniuse.com/#feat=download – ryanm Aug 22 '16 at 17:03
  • 1
    This should be the most upvoted answer since it doesn't involve any other language – Erwan Clügairtz Feb 01 '18 at 11:23
15

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> 
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
6

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

evox
  • 61
  • 1
  • 1
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>
mwopata
  • 36
  • 4
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;
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 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 Oct 05 '10 at 06: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
Ryszard Jędraszyk
  • 2,296
  • 4
  • 23
  • 52
0

Just add the 'download' attribute in the anchor tag

<a href="some.xml" download >Downlad XML</a>
arsh
  • 1,218
  • 8
  • 5