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
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
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"):
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.
Jump to Post— pritaeas 2,276What do you have so far? What do you need help with?
Jump to Post— pritaeas 2,276Loop the array, get the files.
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?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.