Download archive from ASP.NET web application
Download archive from the server
It is a common feature for web applications to download a file from your web server. To reduce data size transferred over wire you may want to compress the file. It is convenient for a user to choose the format of the archive to download. This sample gives you some insights on how to manage it.
Prepare ASP.NET Web Application
Using Visual Studio compose ASP.NET Core Web Application. Let it be a simple Web Application with Razor Pages.
Using NuGet Package Manager you include two packages for your project:
Aspose.Zip for compression and
Aspose.BarCode for generation sample data.
Find page Index.cshtml
within your Solution Explorer. Now, add a form to that page with a submit button within it. It needed to initiate download request by user.
Add to that page following HTML markup:
1<form method="post">
2 <input type="submit" value="Download" />
3</form>
List archive formats
For this sample, we chosen three of
archive formats Aspose.ZIP supports: ZIP, Bzip2, 7z.
Compose enumeration for those formats marking its members with
DisplayAttribute
The enumeration code will be:
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 look to the index page. You’ll see something like that:
Handling user request
So,the user specify desired archive format and hits “Download”. How to handle his request on the server-side? Using ASP.NET approach we need to compose an appropriate OnPost
method to Index.cshtml.cs
source. Here is the draft of the method:
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 }
So for the requested archive type we must prepare the corresponding archive from sample data (result
variable) and respond with Microsoft.AspNetCore.Mvc.FileStreamResult
having the proper MIME type.
Generating sample data
We use Aspose.BarCode library to generate BMP image of barcode using this instruction. The snippet of data stream generation:
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 we have a raw data stream from the GenerateBarcode
method. Compress it appropriately in every case. Below is the final 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 }