如何使用C#来下载XML文件中的asp.net [英] How to download xml file in asp.net using C#

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

问题描述

我使用的是Web应用程序的 asp.net MVC3与。我是新来MVC3。我有我的网页上的下载按钮。当我去点击下载按钮,我想能够打开该XML文件

我曾尝试在某些code变化的的ActionResult ,但我没有得到打开的文件。通过使用下面提到code我得到一个下载弹出。每当我要打开的文件,我得到一些例外,如下图所示。任何人都可以请帮我做到这一点的?帮我解决这个问题。 : - )

先谢谢了。

在我的控制器code是:

 公共FileResult下载(字符串ID)
{
    字符串FID = Convert.ToString(ID);    。VAR模型= service.GetAllDefinitions()第一个(X => x.ID == ID);
    VAR definitionDetails =新StatisticDefinitionModel(模型);
    VAR定义= definitionDetails.ToXml;    字符串文件名= definitionDetails.Name +的.xml
    字符串的contentType =文/ XML;    返回文件(Encoding.Uni code.GetBytes(定义),contentType中,文件名);
   }

唯一的例外是:

 无法显示的XML页面
使用XSL样式表无法查看XML输入。请更正错误,然后单击刷新按钮,或稍后重试。
-------------------------------------------------- ------------------------------一个名字开始与无效字符。错误处理资源'的file:/// C:/用户/ ASUB /下载/ fileNamegd ...<


解决方案

您不能传递一个字节数组,你需要一个流。刚刚从你的定义通流:

 公共FileResult下载(字符串ID){
        字符串FID = Convert.ToString(ID);        。VAR模型= service.GetAllDefinitions()第一个(X => x.ID == ID);
        VAR definitionDetails =新StatisticDefinitionModel(模型);
        VAR定义= definitionDetails.ToXml;        字符串文件名= definitionDetails.Name +的.xml
        字符串的contentType =文/ XML;        返回文件(新的MemoryStream(Encoding.Uni code.GetBytes(定义)),contentType中,文件名);
    }

I am using an web application asp.net with mvc3. I am new to mvc3. I am having a download button on my webpage. when i am going to click the download button ,I want to able to open that XML File.

I have tried with some code changes in ActionResult but I didnt get the file opened. By using the below mentioned code I am getting a download popup. whenever i am going to open the file I am getting some exception as shown below. Can anyone please help me for do this? Help me to resolve this problem . :-)

Thanks in Advance.

My code in controller is:

public FileResult Download(string id)
{
    string fid = Convert.ToString(id);

    var model = service.GetAllDefinitions().First(x => x.ID == id);
    var definitionDetails = new StatisticDefinitionModel(model);
    var definition = definitionDetails.ToXml;

    string fileName = definitionDetails.Name + ".xml";
    string contentType = "text/xml";

    return File(Encoding.Unicode.GetBytes(definition), contentType, fileName);
   }

The exception is:

The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 


--------------------------------------------------------------------------------

A name was started with an invalid character. Error processing   resource 'file:///C:/Users/asub/Downloads/fileNamegd...

<

解决方案

You can't pass a byte array, you need a Stream. Just pass a stream from your definition:

    public FileResult Download(string id) {
        string fid = Convert.ToString(id);

        var model = service.GetAllDefinitions().First(x => x.ID == id);
        var definitionDetails = new StatisticDefinitionModel(model);
        var definition = definitionDetails.ToXml;

        string fileName = definitionDetails.Name + ".xml";
        string contentType = "text/xml";

        return File(new MemoryStream(Encoding.Unicode.GetBytes(definition)), contentType, fileName);
    }

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

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