views:

41

answers:

4

Hi All,

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

Help would be greatly appreciated

Thanks

A: 

Use Response.TransmitFile(). Here is a really great example to get you going.

JonVD
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;
    }
Orod Semsarzadeh
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
+2  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> 
eed3si9n
Is this supported by all browsers?
sje397
I can't guarantee *all* browsers, but it should work on most browsers if they respect HTTP.
eed3si9n
A: 

At the bottom of this screen is a question feed anchor whose href is http://stackoverflow.com/feeds/question/3860750 which happens to be XML. If I right click, Save Link As... I get a file titled 3860750 containing:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"  
   xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"  
   xmlns:re="http://purl.org/atompub/rank/1.0"&gt;
<title type="text">How to download an XML without the browser 
opening it in another tab - Stack Overflow </title>
...
msw