Hi,
I am developing a website. I have a folder containing some images. Please somebody help me with the code so that the users can download the folder when they click on the download button.
Bincy
Hi,
I am developing a website. I have a folder containing some images. Please somebody help me with the code so that the users can download the folder when they click on the download button.
Bincy
You cannot download a folder as-is over HTTP. The common pattern is to zip the folder on the fly and stream the archive to the client. In ASP.NET (4.5+), you can create a ZIP directly to the response stream so you do not write temp files to disk:
using System.IO;
using System.IO.Compression;
using System.Web;
public static void DownloadAlbum(string dirVirtualPath, string zipFileName)
{
var ctx = HttpContext.Current;
string dir = ctx.Server.MapPath(dirVirtualPath);
if (!Directory.Exists(dir)) { ctx.Response.StatusCode = 404; return; }
ctx.Response.Clear();
ctx.Response.BufferOutput = false; // good for large archives
ctx.Response.ContentType = "application/zip";
ctx.Response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName + ".zip");
using (var zip = new ZipArchive(ctx.Response.OutputStream, ZipArchiveMode.Create, leaveOpen: true))
{
foreach (var path in Directory.EnumerateFiles(dir, "*.*", SearchOption.TopDirectoryOnly))
{
var entry = zip.CreateEntry(Path.GetFileName(path), CompressionLevel.Optimal);
using (var src = File.OpenRead(path))
using (var dest = entry.Open())
src.CopyTo(dest);
}
}
ctx.ApplicationInstance.CompleteRequest();
} Notes:
TransmitFile for better performance.Content-Type (for example video/mp4) and consider supporting HTTP range requests; static files via IIS handle ranges automatically. See Content-Disposition and ZipArchive for details.Jump to Post— sknake 1,622Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownloadForm.aspx.cs" Inherits="daniweb.asp.DownloadForm" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="
Jump to Post— asprin 14Hi Bincy,
I'm not an expert in Web Apps but I'm pretty sure, folders are not downloadable. You should first add it to a zip file and then download the zip. This should allow the user to download all your images.
Hope that helped :-)!I think you are absolutely …
Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownloadForm.aspx.cs" Inherits="daniweb.asp.DownloadForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html> Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace daniweb.asp
{
public partial class DownloadForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
const string fName = @"C:\picture.bmp";
FileInfo fi = new FileInfo(fName);
long sz = fi.Length;
Response.ClearContent();
Response.ContentType = MimeType(Path.GetExtension(fName));
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}",System.IO.Path.GetFileName(fName)));
Response.AddHeader("Content-Length", sz.ToString("F0"));
Response.TransmitFile(fName);
Response.End();
}
public static string MimeType(string Extension)
{
string mime = "application/octetstream";
if (string.IsNullOrEmpty(Extension))
return mime;
string ext = Extension.ToLower();
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (rk != null && rk.GetValue("Content Type") != null)
mime = rk.GetValue("Content Type").ToString();
return mime;
}
}
} Hi,
Thanks for your help. But i got a problem. The code works well with a single image(picture.bmp) in C drive. But i have a folder containing 6 pictures and the image folder is uploaded in the webserver. I need the code for downloading that folder when the online users click on the download album button.
Bincy
Hi Bincy,
I'm not an expert in Web Apps but I'm pretty sure, folders are not downloadable. You should first add it to a zip file and then download the zip. This should allow the user to download all your images.
Hope that helped :-)!
Hi Bincy,
I'm not an expert in Web Apps but I'm pretty sure, folders are not downloadable. You should first add it to a zip file and then download the zip. This should allow the user to download all your images.
Hope that helped :-)!
I think you are absolutely correct here. Zipping the folder would solve your problem.
thank you the code worked just perfect.. helped me to complete my website for downloading notes.
Try this example:
public void TheDownload(string path)
{
System.IO.FileInfo toDownload = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename=" + toDownload.Name);
HttpContext.Current.Response.AddHeader("Content-Length",
toDownload.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(patch);
HttpContext.Current.Response.End();
}
The implementation is done in the follows:
TheDownload("@"c:\Temporal\Test.txt"");
can we apply for video format files?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.