Alloi
Alloi

Reputation: 683

How to download an XML without the browser opening it in another tab

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

Upvotes: 21

Views: 64599

Answers (7)

arsh
arsh

Reputation: 1258

Just add the 'download' attribute in the anchor tag

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

Upvotes: 0

Ryszard Jędraszyk
Ryszard Jędraszyk

Reputation: 2412

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

Upvotes: 0

mwopata
mwopata

Reputation: 36

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>

Upvotes: 1

evox
evox

Reputation: 71

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

Upvotes: 7

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13928

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>

Upvotes: 18

Orod Semsarzadeh
Orod Semsarzadeh

Reputation: 149

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

Upvotes: 0

Eugene Yokota
Eugene Yokota

Reputation: 95624

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> 

Upvotes: 16

Related Questions