Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you force the browser to download an xml file?

This is my problem. I load xml from my database and push it to the client using code. But the problem is that the browser automatically opens that xml instead of offering it as a download.

Is there a way to force your browser to download that file and not showing it?

I'm working in a C#, Asp.net environment (with IIS7).

Thx

like image 796
user29964 Avatar asked Dec 16 '09 07:12

user29964


People also ask

How do I download an XML file in Firefox?

Open about:preferences -> Applications. On Extensible Markup Language (xml) action, select "Save File".

How do I export XML from Chrome?

Simply click the File button (the 3 lines), and click Save Page As. For example, I went to xml-sitemaps.com/sitemap.xml and clicked Save Page As. It saved as XML to my local machine and loaded as such.


1 Answers

protected void DisplayDownloadDialog()
{
    Response.Clear();
    Response.AddHeader(
        "content-disposition", string.Format("attachment; filename={0}", "filename.xml"));

    Response.ContentType = "application/octet-stream";

    Response.WriteFile("FilePath");
    Response.End();
}

This will force to download the file and not display in the browser.

This will work for any file types without requiring to specify any special MIME type.

like image 95
this. __curious_geek Avatar answered Oct 06 '22 17:10

this. __curious_geek