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

后端 未结 3 2020
误落风尘
误落风尘 2020-12-30 08:11

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

相关标签:
3条回答
  • 2020-12-30 08:31
    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.

    0 讨论(0)
  • 2020-12-30 08:31

    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) 
    
    0 讨论(0)
  • 2020-12-30 08:45

    Add a content-disposition: attachment header.

    0 讨论(0)
提交回复
热议问题