Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Niels Lynggaard 175 posts 472 karma points
    Aug 30, 2018 @ 09:47
    Niels Lynggaard

    Create a SurfaceController Action to zip some files and return them..

    Hi All.

    I'm working on a solution to combine a few files into a .zip and return it to the browser.

    I'm looking to use the included zip-library ICSharpCode.SharpZipLib.Zip;

    Any chance someone has an example of this? What kind of result should my action return?

    Cheers, Niels

  • Nik 1197 posts 4973 karma points MVP 2x c-trib
    Aug 30, 2018 @ 15:06

    Hi Niels,

    There are a few bits an pieces to consider, but basically you need to return the File, however there are a couple of different ways of doing this.

    You can return a FilePathResult, as demonstrated here:

    You can also return a FileStreamResult discussed in the question here:

    Or you can roll your own HttpResponseMessage as demonstrated here:

    There is also a FileContentResult which I couldn't find an example of quickly (sorry)

    Each approach has different benefits and I think in the past I've generally used a FileContentResult to return files from controllers.



  • Niels Lynggaard 175 posts 472 karma points
    Sep 18, 2018 @ 13:36
    Niels Lynggaard

    Thanx for your input, Nik!

    I ended up using a FileContentResult like this;

    return (ActionResult)this.File(this.GetFile(this.Server.MapPath("/zip/" + (object)productPageID + "")), "application/octet-stream", "");

    the GetFile method returns a byte-array like this;

    private byte[] GetFile(string s)
            FileStream fileStream = System.IO.File.OpenRead(s);
            byte[] buffer = new byte[fileStream.Length];
            if ((long)fileStream.Read(buffer, 0, buffer.Length) != fileStream.Length)
                throw new IOException(s);
            return buffer;

    This works like a charm :)

Please Sign in or register to post replies

Write your reply to: