Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
21 May
2023

Download Files as Zip file in Asp.Net

by Shubham Batra

64

Download Attachment


Download Zip Files

For downloading the files in zip format we will use ZipArchieve class. For using this we have to install following nuget package.

Once you install this package you can go to controller and add following namespace


using System.IO;
using System.IO.Compression;

Now, we have to add following code for download files as zip


public ActionResult DownloadFilesAsZip()
        {
            // Get the paths of the files to be included in the zip
            string[] filePaths = new string[]
            {
            Server.MapPath("~/Content/Screenshot_1.png"),
            Server.MapPath("~/Content/Screenshot_2.jpg"),
            Server.MapPath("~/Content/Screenshot_3.png")
            };

            // Create a memory stream to store the zip file
            MemoryStream memoryStream = new MemoryStream();

            using (ZipArchive zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
            {
                // Add each file to the zip archive
                foreach (string filePath in filePaths)
                {
                    string fileName = Path.GetFileName(filePath);
                    zipArchive.CreateEntryFromFile(filePath, fileName);
                }
            }

            // Set the position of the memory stream back to the beginning
            memoryStream.Position = 0;

            // Return the zip file for download
            return File(memoryStream, "application/zip", "Files.zip");
        }

So, in the filepaths you can add any file paths that you want to add inside zip file.

Now we will add a link on the view to call this method.

@Html.ActionLink("Download Files as Zip", "DownloadFilesAsZip", "Home")

So, Now you can run the application and you have to click on the download button and it will download files as zip.

In the screenshots you can see the files getting downloaded as zip file.

In this example, the DownloadFilesAsZip action method takes an array of file paths and creates a zip archive using ZipArchive. Each file is added to the archive using CreateEntryFromFile

The resulting zip archive is then stored in a MemoryStream, and the position of the stream is set back to the beginning before returning the file using the File method. The File method takes the MemoryStream, the content type (in this case, "application/zip"), and the desired file name for the download.

So this is how we can Download Files as Zip file in Asp.Net.

  • |
  • ZipArchieve , Download Files as Zip , AspNet MVC

Comments

Follow Us On Social Media - Like Us On Facebook

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

Continue with Downloading

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2023 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier