ASP.NET Web ????????????????????????

??????????????????

Web ??????????????????????Web ??????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????

ASP.NET Web ???????????

Visual Studio ??????ASP.NET Core Web ?????????????????? Razor Pages ???????? Web ????????????????? NuGet ????? ??????????????????? 2 ????????????????? Aspose.Zip ????? ??????? Aspose.BarCode ???
??????? ???????????Index.cshtml??????????????????????????????????????????????????????????????????
?? HTML ???????????????????

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

???????????

??????????? ??????? Aspose.ZIP ??????? 3 ????????: ZIP?Bzip2?7z? DisplayAttribute ???????????????????????
????????????????

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

???Web ????????????? ???????????????? GetEnumSelectList HTML ?????????????????????????????????????? ???????Index.cshtml???????????????????

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

??????????????????? ?????????????????????????

????????????

???????????????????????????????????????????????????????????????????????????? ASP.NET ????????????Index.cshtml.cs?????????OnPost??????????????????????????????????

 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  }

???????????????? ???????????? ??? (result ??) ??????????????????? MIME ?????? Microsoft.AspNetCore.Mvc.FileStreamResult ?????????????

??????????

Aspose.BarCode ??????????? ???? ??????????? BMP ????????? ??? ?????????????:

 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  }

????????

?????GenerateBarcode????????????? ?????????????????????????????????????? OnPost ???????

 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.