Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MVC ActionResult that returns multiple files

Tags:

c#

asp.net-mvc

Can an MVC ActionResult return multiple files? If so, can it return multiple files, of multiple type?

Examples:
Can an ActionResult return myXMLfile1.xml, myXMLfile2.xml, and myfile3.xml?

Can an ActionResult return myXMLfile4.xml, and myTXTfile1.txt?

How could this be accomplished?

like image 814
A Bogus Avatar asked Jul 12 '13 14:07

A Bogus


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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

like image 153
Felipe Oriani Avatar answered Sep 30 '22 00:09

Felipe Oriani