简体   繁体   English

如何在 ASP.NET Core MVC 中下载多个文件

[英]How to download multiple files in ASP.NET Core MVC

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:这是我的 controller 代码:

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?当我单击上传下载时,上传是否正在存储在数据库 2 个表中?

Getting document single id获取文档单一ID

public Files GetDocumentbyDocumentId(int documentId)
{
    var list = (from doc in _auc.Files
                where doc.DocumentId == documentId
                select doc).FirstOrDefault();
    return list;
}

To download multiple file, you can create a zip file and add the multiple files inside it.要下载多个文件,您可以创建一个 zip 文件并在其中添加多个文件。 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.在此示例中,我将查询 Products 表并获取它们的图像内容,然后使用 Zip 文件下载它们。

    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:产品 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: output如下:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM