How to send XML file to client in ASP.NET MVC

会有一股神秘感。 提交于 2019-12-23 11:21:10

问题


In an ASP.NET MVC I have a database table. I want to have a button on some view page, if some user clicks that button I my application will generate XML file containing all rows in the database. Then the file containing XML should be sent to the client so that the user will see a download pop-up window.

Similarly I want to allow user to upload an XML file whose content will be added to the database.

What's the simplest way to let the user upload and download file ?

Thanks for all the answers

EDIT: This is my approach:

public FileContentResult Download() {
        if(model.Series.Count() < 1) {
            byte[] content = new byte[0];
            return new FileContentResult(content, "Series");
        }
        XmlSerializer serializer = new XmlSerializer(model.Series.FirstOrDefault().GetType());

        MemoryStream xmlStream = new MemoryStream();
        foreach (Series s in model.Series) {
            serializer.Serialize(xmlStream, s);
        }

        byte[] content2 = new byte[xmlStream.Length];
        xmlStream.Position = 0;
        xmlStream.Read(content2, 0, (int) xmlStream.Length);

        return File(content2, "Series");
}

Where model is DataContext. Howewer this does not work. When I try to download the data I get this error:

XML Parsing Error: junk after document element
Location: http://localhost:1399/Xml/Download
Line Number 7, Column 10:</Series><?xml version="1.0"?>
---------^

回答1:


for download part, you could use FileStreamResult

This page has examples for upload and download; check it out.




回答2:


An XML document can only have one top level element. After the end of the element, you cannot have anything else. It looks like after the "</Series>" element you have "<?xml version="1.0>", which is invalid.



来源:https://stackoverflow.com/questions/2905426/how-to-send-xml-file-to-client-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!