1,105,290 Community Members

file download in asp.net using c# code

Member Avatar
bincy_2050
Newbie Poster
2 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
sknake
Senior Poster
3,957 posts since Feb 2009
Reputation Points: 1,620 [?]
Q&As Helped to Solve: 747 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

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;
    } 
  }
}
Member Avatar
bincy_2050
Newbie Poster
2 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ladybix
Newbie Poster
1 post since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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 :-)!

Member Avatar
asprin
Light Poster
44 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

aneeshammu
Newbie Poster
1 post since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

thank you the code worked just perfect.. helped me to complete my website for downloading notes.

Member Avatar
Angel Armando
Newbie Poster
4 posts since Dec 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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"");

You
Post:
Start New Discussion
View similar articles that have also been tagged: