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
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.