Answer the question
In order to leave comments, you need to log in
How to download files using MVC 3?
Hello. I receive a list of files from the service, how can I offer the user to download them.
The controller code is something like this
public ActionResult GetFiles()
{
var files = Client.GetFiles();
}
public class File
{
public string Name { get; private set; }
public byte[] FileData { get; private set; }
}
Answer the question
In order to leave comments, you need to log in
You cannot send several files to the client at once. You can have one, something like this:
public FileResult GetFile()
{
var file = Client.GetFile();
return File(file.FileData, System.Net.Mime.MediaTypeNames.Application.Octet, file.Name);
}
public FileResult GetFiles()
{
var files = Client.GetFiles();
var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
foreach(var file in files)
zip.AddEntry(file.Name, file.FileData);
zip.Save(outputStream);
}
outputStream.Position = 0;
return File(outputStream, "application/zip", "files.zip");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question