I have a standard ASP .NET Core MVC application. I’m using dropzone JS plugin to upload files and send them to controller’s action that works in a way, that every time a file is uploaded - an action is called. So for example, if 5 files are uploaded - the HttpPost action will be called 5 times. The file is passed to action as a parameter using [FromForm] IFormFile file.
Now, my task is to write a service for my controller, that will add every uploaded file to ZIP archive and in the end, when user is done and clicked the button - ZIP archive will start downloading on user’s computer. I tried some ideas with Aspose package, but I get the error in the title, here’s how my service looks like:
using Aspose.Zip;
namespace EncryptionUtility.Services;
public class ArchiveHelperService
{
private Archive _archive = new();
private MemoryStream _memoryStream;public void AddFileToZip(string fileName, MemoryStream fileStream) { _archive.CreateEntry(fileName, fileStream); } public MemoryStream DownloadArchive() { _archive.Save(_memoryStream); _archive.Dispose(); return _memoryStream; }
}
And these are controller’s actions:
[HttpPost(“upload”)]
public async Task Upload([FromForm] IFormFile file)
{
var fileName = file.Name;
var stream = new MemoryStream();
await file.CopyToAsync(stream);
stream.Position = 0;
_service.AddFileToZip(fileName, stream);
}[Route("download")] public IActionResult Download() { var archiveStream = _service.DownloadArchive(); return File(archiveStream, "application/zip", "archive.zip"); }
Is this approach too naive? What could be the issue?