Upload archive to ASP.NET web application
Scenario
This article provides you with an example of how to use Aspose libraries for archive decompression and image processing for ASP.NET web application. The user case: site visitor uploads zip archive with images to web application to rotating them all by a specific angle. Rotated images will be displayed on the same web page in response.
Prepare ASP.NET Web Application
Using Visual Studio compose simple ASP.NET Core Web Application with Razor Pages.
Using NuGet Package Manager refer two packages for your project:
Aspose.Zip for decompression and
Aspose.Imaging for image processing.
Find page Index.cshtml
within your Solution Explorer. Now, add a form to that page with enctype="multipart/form-data"
attribute within <form>
tag. It needed to transfer file to web server. Then, add two input fields - one of type file
for uploaded archive, another of type number
to specify rotation angle.
Here is full HTML markup for the form:
1<form method="post" enctype="multipart/form-data">
2 <span>Images archive: </span><input type="file" name="uploadedFile" required="required" />
3 <br />
4 <span>Rotation angle: </span><input type="number" value="30" min="0" max="360" name="rotateDegree" required="required" />
5 <br />
6 <input type="submit" value="Upload" />
7</form>
Some validation attributes have been added as well.
Handling user request
So, the user provides the zip archive with images and submits the form. 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. Within this method we extract
zip archive using
the appropriate constructor, then rotate each image. Here is the draft of the method:
1public void OnPost(IFormFile uploadedFile, int rotateDegree) {
2 using (Archive archive = new Archive(uploadedFile.OpenReadStream())) {
3 foreach (ArchiveEntry entry in archive.Entries) {
4 // Extract image and rotate it.
5 }
6 }
7}
Rotate images
We use Aspose.Imaging library to rotate an image by particular angle on the fly angle following this instruction. The snippet of rotation code:
1 using (MemoryStream rotated = new MemoryStream())
2 {
3 using (RasterImage image = (RasterImage)Image.Load(extracted))
4 {
5 // Before rotation, the image should be cached for better performance
6 if (!image.IsCached)
7 image.CacheData();
8
9 image.Rotate(rotateDegree, true, Color.CornflowerBlue);
10 image.Save(rotated);
11 }
12 }
Page Model and Image Rendering
We need to store bytes of proceeded pictures within the page model to render those pictures. Add property public List<byte[]> ImageBytes { get; private set; }
to IndexModel
.
We fill this list with proceeded image bytes. To show them on the web page we use
data URI We need to convert image bytes to base64 string.
Here is a rendering Razor code at Index.cshtml
1@{
2if (Model.ImageBytes != null && Model.ImageBytes.Count > 0) {
3 <h4>Rotated images:</h4>
4 foreach (byte[] image in Model.ImageBytes) {
5 <img src="data:image;base64,@Convert.ToBase64String(image)" width="150"/>
6 }
7 }
8}
Finishing response
So, put it all together. Each entry of the archive has been decompressed, then an image composed from these bytes, and finally rotated.
We do not validate user file in this sample. In real-world applications, you should verify an uploaded archive and its content.
Below is the final OnPost
method.
1 public void OnPost(IFormFile uploadedFile, int rotateDegree)
2 {
3 ImageBytes = new List<byte[]>();
4
5 using (Archive archive = new Archive(uploadedFile.OpenReadStream()))
6 {
7 foreach (ArchiveEntry entry in archive.Entries)
8 {
9 if (entry.IsDirectory)
10 continue;
11
12 using (MemoryStream extracted = new MemoryStream())
13 {
14 entry.Open().CopyTo(extracted);
15 extracted.Seek(0, SeekOrigin.Begin);
16 using (MemoryStream rotated = new MemoryStream())
17 {
18 using (RasterImage image = (RasterImage)Image.Load(extracted))
19 {
20 if (!image.IsCached) // Before rotation, the image should be cached for better performance
21 image.CacheData();
22
23 image.Rotate(rotateDegree, true, Color.CornflowerBlue);
24 image.Save(rotated);
25 }
26
27 this.ImageBytes.Add(rotated.ToArray());
28 }
29 }
30 }
31 }
32 }