Here is the
.aspx page UI part, please register Ajax toolkit if you don’t want to be post
back...Otherwise you can remove below Script Manage and their respective...
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server">asp:ScriptManager>
<div>
<asp:UpdateProgress ID="updateprogress1" runat="server" DisplayAfter="2000">
<ProgressTemplate>
An upload is in progress ...
ProgressTemplate>
asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Choose File..<asp:FileUpload ID="fuImage" runat="server" />
<asp:Label ID="lblMessage" runat="server">asp:Label>
ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
Triggers>
asp:UpdatePanel>
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" />
div>
<div>Download
the files<br
/>
<asp:DropDownList ID="ddlUploadedFiles" runat="server">
asp:DropDownList><br />
<asp:Button ID="btnDownload" runat="server" Text="Download"
onclick="btnDownload_Click" />
div>
form>
Write down the below code in .aspx.cs
page
using System;
using
System.Collections;
using
System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using
System.Web.Security;
using System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.IO;
public partial class FileUpload : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
//bind
uploaded files from upload directory in DropDownList
BindUploadedFiles();
}
}
// upload button click event
protected void btnUpload_Click(object
sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
if
(fuImage.HasFile)
{
try
{
fuImage.SaveAs(Server.MapPath("~/Upload/" + fuImage.FileName));
lblMessage.Text = "File uploaded successfully!";
}
catch
(Exception ex)
{
lblMessage.Text = ex.Message;
}
}
}
// bind uploaded files in dropdown list..
protected void BindUploadedFiles()
{
DirectoryInfo
di = new DirectoryInfo(Server.MapPath("~/Upload/"));
FileInfo[]
fi = di.GetFiles("*", SearchOption.AllDirectories);
foreach
(FileInfo fileExistInDir in fi)
{
ddlUploadedFiles.Items.Add(fileExistInDir.Name);
}
}
// button download click event..
protected void btnDownload_Click(object
sender, EventArgs e)
{
foreach
(ListItem selFile in
ddlUploadedFiles.Items)
{
if
(ddlUploadedFiles.SelectedIndex >=0)
{
string
filePath = Server.MapPath("~/upload/"
+ ddlUploadedFiles.SelectedValue);
string
fileName = Path.GetFileName(filePath);
Response.Clear();
Response.ContentType = "application/jpg";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(filePath) + "\"");
Response.TransmitFile(filePath);
Response.End();
}
}
}
}
Please feel free to comment/ feedback..
Comments