如何在ASP.NET MVC中发送XML文件给客户端 [英] How to send XML file to client in ASP.NET MVC
问题描述
同样,我想允许用户上传一个XML文件其内容将被添加到数据库中。
让用户上传和下载文件的最简单的方法是什么?
感谢所有答案
编辑:
这是我的方法:
public FileContentResult下载(){
if(model.Series.Count()< 1){
byte [] content = new byte [ 0];
返回新的FileContentResult(content,Series);
}
XmlSerializer serializer = new XmlSerializer(model.Series.FirstOrDefault()。GetType());
MemoryStream xmlStream = new MemoryStream();
foreach(model.Series中的系列){
serializer.Serialize(xmlStream,s);
}
byte [] content2 = new byte [xmlStream.Length];
xmlStream.Position = 0;
xmlStream.Read(content2,0,(int)xmlStream.Length);
返回文件(content2,系列);
}
其中模型是DataContext。这个没有办法。当我尝试下载数据时,我收到这个错误:
XML解析错误:文档元素
后的垃圾位置: http:// localhost:1399 / Xml / Download
行号7,第10列:< / Series><?xml version =1.0?>
--------- ^
下载部分,您可以使用 FileStreamResult
此页面具有上传和下载的示例;检查出来。
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"?>
---------^
for download part, you could use FileStreamResult
This page has examples for upload and download; check it out.
这篇关于如何在ASP.NET MVC中发送XML文件给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!