![]() Return File(memoryStream, "application/zip", "download.zip") Īfter resetting the stream, I'm returning it as part of the File-method. It's wrapped in a MemoryStream since we want to return a file stream from the method: memoryStream.Position = 0 The code uses the ZipArchive class available in. ZipArchive.CreateEntryFromFile(file, Path.GetFileName(file)) Using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) Next, we need the zipping code: using (var memoryStream = new MemoryStream()) In the following code, I'll hard-code some paths, but each file could come from the client, from a database, or something third: List files = new List ![]() ![]() Start by building a list of files to zip. The code doesn't compile yet, so let's fix that. Start by declaring the method without a body: In real-life, input parameters would often be required for the backend to know what to zip, but I'll leave that part out for simplicity. I'm picking the MVC template, but none of the zip-related code is specific to MVC.įor this example, I'll create a single endpoint able to zip and download some files. To get started, I'll create a new ASP.NET Core website: dotnet new mvc Zipping the files before downloading them, turned out as a great way of easily implementing multi-file download.NET offers all of the needed features and in this post, I'll show you how to implement it. Written by Thomas Ardal, January 31, 2023įor a recent feature, I had to download a batch of files from an internal website written in ASP.NET Core.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |