V
V
Voucik2014-05-24 23:45:54
C++ / C#
Voucik, 2014-05-24 23:45:54

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();
            
        }

files contains a list (List"<"File">")
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

1 answer(s)
B
Bob Smith, 2014-05-26
@Voucik

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);
}

Or first zip, and then give the archive. More or less like this:
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 question

Ask a Question

731 491 924 answers to any question