从MVC的行动返回多个文件 [英] Returning Multiple Files from MVC Action
问题描述
所以我有一个MVC 3应用程序,具有其中一个文本文件被生成,使用返回一个动作几个地方:
So I've got an MVC 3 application that has a couple places where a text file gets generated and returned in an action using:
return File(System.Text.Encoding.UTF8.GetBytes(someString),
"text/plain", "Filename.extension");
和这部作品惊人。现在我已经得到了在那里我试图返回一个对文件以类似的方式的情况。在视图,我有这样一个动作链接点击这里获取的2个文件,我想这两个文件被下载很像一个文件在上述code片段下载。
and this works fabulously. Now i've got a situation where I'm trying to return a pair of files in a similar fashion. On the view, i have an action link like "Click here to get those 2 files" and i'd like both files to be downloaded much like the single file is downloaded in the above code snippet.
我怎样才能做到这一点?一直在寻找周围颇有几分甚至还没有见过这个问题提出的任何地方...
How can I achieve this? Been searching around quite a bit and haven't even seen this question posed anywhere...
推荐答案
大厦Yogendra辛格的想法,并使用 DotNetZip :
Building on Yogendra Singh's idea and using DotNetZip:
var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
zip.AddEntry("file1.txt", "content1");
zip.AddEntry("file2.txt", "content2");
zip.Save(outputStream);
}
outputStream.Position = 0;
return File(outputStream, "application/zip", "filename.zip");
这篇关于从MVC的行动返回多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!