How to download multiple files in ASP.NET Core MVC

selfie tv 21 Reputation points
2022-09-24T00:30:24.693+00:00

How to download multiple files? If the user sends a single file, display it, and if the user sends multiple files, how to set?

This is my controller code:

public IActionResult DownloadFile(int id)  
{  
    byte[] bytes;   
    string fileName, contentType;  
  
    var model = new List<Files>();  
  
    var getdocument = _documentdata.GetDocumentbyDocumentId(id);  
  
    if (getdocument != null)  
    {  
        fileName = getdocument.Name;  
        contentType = getdocument.FileType;  
        bytes = getdocument.DataFiles;  
  
        return File(bytes, contentType: "application/octet-stream", fileName);  
    }  
       
    return Ok("Can't find the image");  
}  

Upload is working storing in database 2 tables when I click uploaded download ?

Getting document single id

public Files GetDocumentbyDocumentId(int documentId)  
{  
    var list = (from doc in _auc.Files  
                where doc.DocumentId == documentId  
                select doc).FirstOrDefault();  
    return list;  
}  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,594 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,356 Reputation points Microsoft Vendor
    2022-09-24T04:36:55.6+00:00

    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:

    244452-image2.gif

    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

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.