ASP.NET MVC 5 Step by Step: Download Multiple Files in Compressed Format
Introduction
This article explains about how to download multiple files as compressed format using ASP.NET MVC 5 in Step by Step way. There are many ways are using to download multiple files as a zip format but this article explains easy way with step by step process.
Background
We can download multiple files using zip format from differ source to destination location. Download the files from server path and sometimes download from network path. We can see how download files from server path as well as network path. We need to give permission to corresponding folder from where we download or upload weather it is from server or any other network path.
Steps for Download multiple Files as Zip File Format
Step 1
Go to Visual Studio, Open new ASP.Net Web Application and give useful project name. Follow the below screen short.
Step 2
Select MVC template from template window and click ok button.
Step 3
Go to solution explore, under solution explore right click on controller folder and add new controller like below screen shots.
After click controller, Controller window will open and select "MVC Controller- Empty" then click add button. After click add then enter controller Name and click ok.
Step 4
Add class and properties for file information. Below screen short explains how to add class and class properties.
Right click on model folder and click add then click class, finally give useful class name like above screen shorts.
Step 5
Add class properties, after adding class. Class properties help to fetch and store file details.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MutipleFileDownload.Models
{
public class FileInfo
{
public int FileId { get ; set ; }
public string FileName { get ; set ; }
public string FilePath { get ; set ; }
}
}
** **
Step 6
Add another class in model folder for fetch files from corresponding locations. We can fetch file from server path and network path. Add "FileInfo" name space in "FileDownlod" class.
Now add coding for fetching files. Before adding code, add "System.IO" namespace. We can fetch files from server and network path.
Coding For Fetching Files
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MutipleFileDownload;
using System.IO;
namespace MutipleFileDownload.Models
{
public class FileDownloads
{
public List<FileInfo> GetFile()
{
List<FileInfo> listFiles = new List<FileInfo>();
string fileSavePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images");
DirectoryInfo dirInfo = new DirectoryInfo(fileSavePath);
int i = 0;
foreach (var item in dirInfo.GetFiles())
{
listFiles.Add(new FileInfo()
{
FileId = i + 1,
FileName = item.Name,
FilePath = dirInfo.FullName + @"\" + item.Name
});
i = i + 1;
}
return listFiles;
}
}
}
** **
Step 7
Add actions methods in "FileDownloadController". Below screen shot explains how to add action method.
Right click on "FileHome" actions methods and click "Add View" then click "Add" button. Below screenshot explains how add view.
After adding view page, add below code for design download file page.
Step 8
We need two assemblies for compress many files to download, without compress we cannot download many files in simultaneously.
Two assemblies are:
- System.IO.Compression
- System.IO.Compression.FileSystem
Add two assemblies in our solution using following way.
Right on Reference in solution explore, Click "Add Reference" then Reference Manger window will be open. Now expand Assemblies, select Framework and find above mentioned two assemblies then select those assemblies. Finally click Ok button.
Step 9
Now add download methods in corresponding controller. This method is used to merge all files as a compressed format.
Step 10
After Adding above code just compile your code then run your project. Now we can download files from mentioned path from your server.
Download Files from Network Path
We can download files from network path. Only need to do small changes in "FileDownloads" class. Add below code for download from network path.
Coding
using MutipleFileDownload;
using System.IO;
namespace MutipleFileDownload.Models
{
public class FileDownloads
{
public List<FileInfo> GetFile()
{
List<FileInfo> listFiles = new List<FileInfo>();
//Path For download From Network Path.
string fileSavePath = @"\\servername\FileFolderName";
DirectoryInfo dirInfo = new DirectoryInfo(fileSavePath);
int i = 0;
foreach (var item in dirInfo.GetFiles())
{
listFiles.Add(new FileInfo()
{
FileId = i + 1,
FileName = item.Name,
FilePath = dirInfo.FullName + @"\" + item.Name
});
i = i + 1;
}
return listFiles;
}
}
}
Controller Coding
using MutipleFileDownload.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MutipleFileDownload.Controllers
{
public class FileDownloadController : Controller
{
// GET: FileDownload
public ActionResult FileHome()
{
return View();
}
public ActionResult Download()
{
FileDownloads obj = new FileDownloads();
//////int CurrentFileID = Convert.ToInt32(FileID);
var filesCol = obj.GetFile().ToList();
using (var memoryStream = new MemoryStream())
{
using (var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < filesCol.Count; i++)
{
ziparchive.CreateEntryFromFile(filesCol[i].FilePath, filesCol[i].FileName);
}
}
return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
}
}
}
}
Conclusion
This step by step articles helps to learn download multiple files as compressed format in easy way. This is helps to students and those who are newly learn MVC. Using this article steps we can download files from server path and network path. Next part of article explains how to download files from server latest uploaded files.