使用asp.net下载xml文件 [英] download xml file using asp.net

查看:91
本文介绍了使用asp.net下载xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我用c#创建了一个xml文件,我可以将它保存在我机器的任何位置(在这种情况下我保存了它名称为temp.xml的应用程序的根目录,但是我想让用户从浏览器中下载它给出一个类似链接的链接 - >>

click< a href =temp.xmltarget =_ blank> HERE< / a>下载文件。



在Chrome和FireFox中它显示一个新的选项卡在xml文件的正文部分只有一些值,但IE显示整个xml。当有人点击上面的链接时,我想在我的下载文件夹中下载它。



提前感谢您的支持。

hi all,

I have created a xml file using c# and I can save it at any location in my machine (in this situation I have saved it on the root of the application with name "temp.xml"), but I want to let it for the user to download it from their browser buy giving a link like-->
"click <a href="temp.xml" target="_blank">HERE</a> to download the file."

In Chrome and FireFox it show a new tab with only some values in my body part of the xml file but IE shows the whole xml. I want to download it in my download folder when anybody click on the above link.

thanks in advance for your support.

推荐答案

无法直接打开XML文件。你必须编写以下代码才能保存在本地系统上。



XML file can not be directly opened. you have to write following code to save on local system.

<asp:linkbutton id="lb_DownloadXML" runat="server" text="Download file" onclick="lb_DownloadXML_Click" xmlns:asp="#unknown"></asp:linkbutton>







protected void lb_DownloadXML_Click(object sender, EventArgs e)
    {
        string strFullPath = Server.MapPath("~/temp.xml");        
        string strContents = null;
        System.IO.StreamReader objReader = default(System.IO.StreamReader);
        objReader = new System.IO.StreamReader(strFullPath);
        strContents = objReader.ReadToEnd();
        objReader.Close();

        string attachment = "attachment; filename=test.xml";
        Response.ClearContent();
        Response.ContentType = "application/xml";
        Response.AddHeader("content-disposition", attachment);
        Response.Write(strContents);
        Response.End();        
    }





希望这可以帮到你...



Hope this may help you...


这篇关于使用asp.net下载xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆