Hi there,
I have a database which contains the paths of different files, what I want to do is to store the paths in an array to create an array of paths then download these files with one click.

How to do that?
please help

Dani AI

Generated

Two practical patterns for "one-click" download of many files were implied in this thread: zip everything server-side and send a single ZIP, or fetch files client-side and build the ZIP in the browser. was right to point out looping the list — but common gaps under that surface cause "nothing" to be delivered. The checklist at the end helps avoid those traps; below are compact, practical examples and notes.

Server-side (robust for large sets): build the archive on the server and stream it back. This C# MVC-style example uses the BCL ZipArchive and streams each file into the archive so you avoid re-reading names manually:

public ActionResult DownloadZip(IEnumerable<int> ids)
{
    var paths = GetPathsFromDatabase(ids); // absolute server paths
    var ms = new MemoryStream();
    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
    {
        foreach (var p in paths)
        {
            if (!System.IO.File.Exists(p)) continue;
            var entry = archive.CreateEntry(Path.GetFileName(p));
            using (var es = entry.Open())
            using (var fs = System.IO.File.OpenRead(p))
                fs.CopyTo(es);
        }
    }
    ms.Position = 0;
    return File(ms, "application/zip", "files.zip");
}

Client-side (good for smaller files, avoids server CPU): fetch each file as a blob, add to a JSZip instance, then generate and download the zip. See JSZip docs for examples: JSZip.

Checklist / troubleshooting (why people often "get nothing"):

  • Ensure the list of paths is available at download time (persist in Session/ViewState or rebind before the click).
  • Verify the app pool user can read those paths and the paths are correct (virtual vs absolute vs UNC).
  • Confirm controls/IDs used to read paths actually contain the values on postback.
  • Avoid creating huge in-memory zips for very large files; prefer on-disk streaming or a streaming library.
  • Use framework file-return helpers (FileResult / FileStreamResult) instead of brittle response-abort calls.

If posts the exact fix they found, it will help others who hit the same subtle postback or permission issue. For compression details see Microsoft docs on ZipArchive: ZipArchive.

Recommended Answers

All 6 Replies

What do you have so far? What do you need help with?

how to download multiple files with one click, I have the paths in an array

Loop the array, get the files.

i did that but got nothing

this is the code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim i As Integer = 0
            Dim pp() As String = {"F:\New folder (2)\1. pdf", "F:\New folder (2)\2.txt", "F:\New folder (2)\3.docx"}
            Dim files As New List(Of ListItem)()
            For Each filePath As String In pp
                files.Add(New ListItem(pp(i)))
                i = i + 1
            Next
            GridView1.DataSource = files
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Using zip As New ZipFile()
            zip.AlternateEncodingUsage = ZipOption.AsNecessary
            zip.AddDirectoryByName("files")
            For Each row As GridViewRow In GridView1.Rows
                Dim filePath As String = TryCast(row.FindControl("lblFilePath"), Label).Text
                zip.AddFile(filePath, "files")
            Next
            Response.Clear()
            Response.BufferOutput = False
            Dim zipName As String = [String].Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
            Response.ContentType = "application/zip"
            Response.AddHeader("content-disposition", "attachment; filename=" + zipName)
            zip.Save(Response.OutputStream)
            Response.[End]()
        End Using
    End Sub

solved
thanks to myself

Care to share your solution, to help others in the future?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.