user29964
user29964

Reputation: 15990

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

Upvotes: 11

Views: 10866

Answers (3)

this. __curious_geek
this. __curious_geek

Reputation: 43207

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.

Upvotes: 21

Konamiman
Konamiman

Reputation: 50273

This is explained in this article: http://www.xefteri.com/articles/show.cfm?id=8

The key is in this line:

Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name) 

Upvotes: 4

Annie
Annie

Reputation: 6631

Add a content-disposition: attachment header.

Upvotes: 1

Related Questions