MVC 可以ActionResult
返回多个文件吗?如果是这样,它可以返回多个文件,多种类型?
示例:
能否ActionResult
返回 myXMLfile1.xml、myXMLfile2.xml 和 myfile3.xml?
可以ActionResult
返回 myXMLfile4.xml 和 myTXTfile1.txt 吗?
这怎么可能实现?
MVC 可以ActionResult
返回多个文件吗?如果是这样,它可以返回多个文件,多种类型?
示例:
能否ActionResult
返回 myXMLfile1.xml、myXMLfile2.xml 和 myfile3.xml?
可以ActionResult
返回 myXMLfile4.xml 和 myTXTfile1.txt 吗?
这怎么可能实现?
You cannot return multiples files, but, you can compact multiple files in a .zip
file and return this compacted file, for sample, create a custom ActionResult in your project, like this:
public class ZipResult : ActionResult
{
private IEnumerable<string> _files;
private string _fileName;
public string FileName
{
get
{
return _fileName ?? "file.zip";
}
set { _fileName = value; }
}
public ZipResult(params string[] files)
{
this._files = files;
}
public ZipResult(IEnumerable<string> files)
{
this._files = files;
}
public override void ExecuteResult(ControllerContext context)
{
using (ZipFile zf = new ZipFile())
{
zf.AddFiles(_files, false, "");
context.HttpContext
.Response.ContentType = "application/zip";
context.HttpContext
.Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
zf.Save(context.HttpContext.Response.OutputStream);
}
}
}
And use it like this:
public ActionResult Download()
{
var zipResult = new ZipResult(
Server.MapPath("~/Files/file1.xml"),
Server.MapPath("~/Files/file2.xml"),
Server.MapPath("~/Files/file3.xml")
);
zipResult.FileName = "result.zip";
return zipResult;
}
See more here: http://www.campusmvp.net/blog/asp-net-mvc-return-of-zip-files-created-on-the-fly