Saturday 20 January 2018

Download multiple files as Zip Archive (Compressed) file in ASP.Net MVC

Download multiple files as Zip Archive (Compressed) file in ASP.Net MVC
Description:

In this example we explain that how to download multiple file as ZIP archive in MVC Razor. Or how to download multiple file as ZIP file in MVC.or how to compressed file in Asp.Net MVC.multiple files will be downloaded as Zip Achieve file using DotNetZip Library in Asp.Net MVC Razor.

We already explain this example using Asp.Net but here we go through using MVC Razor.so how to download multiple file as Zip Archive in MVC razor.
Model:
  
  public class FileModel
    {
        public string FileName { get; set; }
        public string FilePath { get; set; }
        public bool IsSelected { get; set; }
    }

Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
            List<FileModel> files = new List<FileModel>();
            foreach (string filePath in filePaths)
            {
                files.Add(new FileModel()
                {
                    FileName = Path.GetFileName(filePath),
                    FilePath = filePath
                });
            }

            return View(files);
        }

        [HttpPost]
        publicActionResult Index(List<FileModel> files)
        {
            using (ZipFile zip = new ZipFile())
            {
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                zip.AddDirectoryByName("Files");
                foreach (FileModel file in files)
                {
                    if (file.IsSelected)
                    {
                        zip.AddFile(file.FilePath, "Files");
                    }
                }
                string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    zip.Save(memoryStream);
                    return File(memoryStream.ToArray(), "application/zip", zipName);
                }
            }
        }

View:

@using Zip_Files_MVC.Models
@model List<FileModel>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th></th>
                <th>File Naame</th>
            </tr>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>@Html.CheckBoxFor(m => m[i].IsSelected)</td>
                    <td>
                        @Model[i].FileName
                        @Html.HiddenFor(m => m[i].FilePath)
                        @Html.HiddenFor(m => m[i].FileName)
                    </td>
                </tr>
            }
        </table>
        <br/>
        <input type="submit" value="Download"/>
    }
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment