【问题标题】:How to download xml file in asp.net using C#如何使用 C# 在 asp.net 中下载 xml 文件
【发布时间】:2012-08-08 08:42:31
【问题描述】:

我正在使用带有 mvc3 的 Web 应用程序 asp.net。我是 mvc3 的新手。我的网页上有一个下载按钮。当我要点击下载按钮时,我希望能够打开那个 XML 文件

我已尝试在 ActionResult 中进行一些代码更改,但我没有打开文件。通过使用下面提到的代码,我得到了一个下载弹出窗口。每当我要打开文件时,我都会遇到一些异常,如下所示。谁能帮我做这件事?帮我解决这个问题。 :-)

提前致谢。

我在控制器中的代码是:

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 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...

<

【问题讨论】:

    标签: xml asp.net-mvc asp.net-mvc-3 c#-4.0


    【解决方案1】:

    如果你返回 FileResult 它将是文件,如果你返回字符串它将在浏览器中打开。

    更新: 此代码将返回文件以供下载

    public FileResult GetXmlFile()
    {
        string xml=""; //string presented xml
        var stream = new MemoryStream();
        var writer = XmlWriter.Create(stream);
        writer.WriteRaw(xml);
        stream.Position = 0;
        var fileStreamResult = File(stream, "application/octet-stream", "xml.xml");
        return fileStreamResult;        
    }
    

    【讨论】:

    • 你能给我样品结果吗?
    • 感谢您的精彩代码。我使用了您的代码,但遇到了一些异常,例如“ System.ObjectDisposedException:无法访问已关闭的流”。您能看看这个吗?
    • 哦。我又这样做了。使用它而不“使用”(我更新答案)
    • 我删除了 using 但我仍然在浏览器中遇到一些其他异常,例如“XML 文档必须具有顶级元素。错误处理资源 'file:///C:/Users/asub/Downloads /xml.xml'。”
    • 为什么使用 contentType "text/xml"?你在客户端上用它做什么?您能说明变量定义中的内容吗?
    【解决方案2】:

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

        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);
        }
    

    【讨论】:

    • 我使用上面的代码得到了这个异常“名称以无效字符开头。错误处理资源'file:///C:/Users/asub/AppData/Local/Micros.. .
    • 将名称更改为有效的文件名,您不能使用通常无法在基于 Windows 的操作系统中使用的奇怪字符。使用这样的东西; string fileName = new string((definitionDetails.Name + ".xml").Where(x => !Path.GetInvalidFileNameChars().Contains(x)).ToArray());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多