简体   繁体   中英

How to download all files in a directory in ASP.NET using C#

I am currently learning uploading / downloading / deleting files in C# asp.net. I figured out how to delete every file in a folder with code like this:

protected void DeleteAllFiles(object sender, EventArgs e)
{
    System.IO.DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Output"));
    
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete();
    }

    foreach (DirectoryInfo dir in di.GetDirectories())
    {
        dir.Delete(true);
    }

    Response.Redirect("~/Outputs.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.ToString()));
}

But I can't find anything on how to download all files in a directory. I figured out how to download individual files but I am having trouble with a button that downloads all files in a directory. Surely there is an easy way to do this? I can't find it anywhere else so this is probably a dumb question to ask but any help is appreciated.

It's not possible to send multiple files in a single HTTP request. However, you can create a zip archive of multiple files and send that instead. See this answer for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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