Hi @selfie tv ,
How to download multiple files?
To download multiple file, you can create a zip file and add the multiple files inside it. Refer to the following sample:
In this sample, I will query the Products table and get their image content, then download them using a Zip file.
public IActionResult DownLoadAll()
{
var zipName = $"TestFiles-{DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss")}.zip";
using (MemoryStream ms = new MemoryStream())
{
//required: using System.IO.Compression;
using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
//QUery the Products table and get all image content
_dbcontext.Products.ToList().ForEach(file =>
{
var entry = zip.CreateEntry(file.ProImageName);
using (var fileStream = new MemoryStream(file.ProImageContent))
using (var entryStream = entry.Open())
{
fileStream.CopyTo(entryStream);
}
});
}
return File( ms.ToArray(), "application/zip", zipName);
}
}
Code in the Index page:
<a asp-controller="Product" asp-action="DownLoadAll">DownLoad All Images</a>
Products model:
public class Product
{
[Key]
public int ProId { get; set; }
public string ProName { get; set; }
public string ProImageName { get; set; }
public byte[] ProImageContent { get; set; } //Image content.
public string ProImageContentType { get; set; }
//you can add another properties
}
The output as below:
If the user sends a single file, display it, and if the user sends multiple files, how to set?
How do you display the files? Using a new browser tab or a web page?
If you want to display the file using the browser, you can view the single file, for the multiple file, generally it will down it via a zip file, then to view them, you can unzip the file and view them on local.
If you are display the files using a web page, in the web page, you can use a for/foreach statement to loop the files, and display them one by one.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion