Download archive from ASP.NET web application

Download archive from the server

It is common for web applications to allow users to download files from the server. To minimize data transfer size, you may want to compress these files. It is convenient for users to choose the archive format before downloading. This sample demonstrates how to implement this feature.

Prepare ASP.NET Web Application

Create an ASP.NET Core Web Application with Razor Pages using Visual Studio. Add the Aspose.Zip package for compression and Aspose.BarCode for generating sample data via the NuGet Package Manager.
Find the page Index.cshtml within your Solution Explorer. Open Index.cshtml and add a form with a submit button to initiate the download request:

1<form method="post">    
2    <input type="submit" value="Download" />
3</form>

List archive formats

For this example, we choose three archive formats supported by Aspose.Zip: ZIP, Bzip2, and 7z. Define an enumeration for these formats and annotate its members with the DisplayAttribute

 1  public enum ArchiveFormat
 2  {
 3      [Display(Name = "ZIP")]
 4      Zip,
 5
 6      [Display(Name = "Bzip2")]
 7      Bz2,
 8
 9      [Display(Name = "7z")]
10      SevenZip
11  }

Next, you need to create a dropdown list within the web form. It is pretty simple with GetEnumSelectList Html helper - no need to generate items with a loop. Just put inside your form on Index.cshtml following snippet:

1<select name="archiveFormat" asp-items="Html.GetEnumSelectList<ArchiveFormat>()"></select>

Run the application and view the index page. You should see a dropdown list with available formats and a Download button.

Handling user request

When the user selects an archive format and clicks “Download”, handle the request on the server-side by defining an OnPost method in Index.cshtml.cs:

 1 
 2
 3  public FileStreamResult OnPost([FromForm] ArchiveFormat archiveFormat)
 4  {
 5    switch (archiveFormat)
 6    {
 7      case ArchiveFormat.Zip:
 8        ...
 9        return new FileStreamResult(result, "application/zip") {FileDownloadName = "barcode.zip"};
10      case ArchiveFormat.Bzip2:
11        ...
12        return new FileStreamResult(result, "application/x-bzip2") {FileDownloadName = "barcode.bmp.bz2"};
13      case ArchiveFormat.SevenZip:
14        ...
15        return new FileStreamResult(result, "application/x-7z-compressed") {FileDownloadName = "barcode.7z"};                           
16    }
17  }

Generate the appropriate archive from the sample data and respond with a Microsoft.AspNetCore.Mvc.FileStreamResult having the correct MIME type.

Generating sample data

Use the Aspose.BarCode library to generate a BMP image of a barcode (see barcode generation instructions. Here is a code snippet for generating the data stream:

 1  private Stream GenerateBarcode()
 2  {
 3    var generator = new Aspose.BarCode.Generation.BarcodeGenerator(
 4        Aspose.BarCode.Generation.EncodeTypes.Pdf417, "This is a test code text. \n Second line \n third line.");
 5              
 6    generator.Parameters.Barcode.XDimension.Millimeters = 0.6f;
 7    generator.Parameters.Barcode.BarHeight.Millimeters = 1.2f;
 8
 9    MemoryStream result = new MemoryStream();
10    generator.Save(result, Aspose.BarCode.Generation.BarCodeImageFormat.Bmp);
11    result.Seek(0, SeekOrigin.Begin);
12    return result;
13  }

Finishing response

Now that we have the barcode stream from the GenerateBarcode method, compress it using the selected archive format. Below is the final version of the OnPost method:

 1  public FileStreamResult OnPost([FromForm] ArchiveFormat archiveFormat)
 2  {
 3      using (var barcode = this.GenerateBarcode())
 4      {
 5          var result = new MemoryStream();
 6          switch (archiveFormat)
 7          {
 8              case ArchiveFormat.Zip:
 9                  using (Archive a = new Archive())
10                  {
11                      a.CreateEntry("barcode.bmp", barcode);
12                      a.Save(result);
13                  }
14                  
15                  result.Seek(0, SeekOrigin.Begin);
16                  return new FileStreamResult(result, "application/zip") {FileDownloadName = "barcode.zip"};
17              case ArchiveFormat.Bzip2:
18                  using (Bzip2Archive a = new Bzip2Archive())
19                  {
20                      a.SetSource(barcode);
21                      a.Save(result);
22                  }
23
24                  result.Seek(0, SeekOrigin.Begin);
25                  return new FileStreamResult(result, "application/x-bzip2") {FileDownloadName = "barcode.bmp.bz2"};
26              case ArchiveFormat.SevenZip:
27                  using (SevenZipArchive a = new SevenZipArchive())
28                  {
29                      a.CreateEntry("barcode.bmp", barcode);
30                      a.Save(result);
31                  }
32
33                  result.Seek(0, SeekOrigin.Begin);
34                  return new FileStreamResult(result, "application/x-7z-compressed") {FileDownloadName = "barcode.7z"};
35              default:
36                  throw new ArgumentOutOfRangeException(nameof(archiveFormat));
37          }
38      }
39  }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.