Sunday, 24 July 2011

Uploading and Deleting Files via Form in ASP.NET and C#


This tutorial will show how we can upload files to a web server and also use a GridView control to delete files as well. C# version.

Quite often, we want to allow uploading of files to a web server, which enables new content to be loaded much quicker & easier than using FTP. ASP.NET makes this easy for us to do, but what happens when we want to delete files from a web server, too? Well ASP.NET, of course, has an answer for this too. We can use a GridView to display files in a certain directory, and allow a user to delete these files.
First, we need the following assembly reference:

using System.IO;

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

We need to add a FileUpload control and a GridView:

<form id="form1" runat="server">
<asp:Label ID="labelStatus" runat="server"></asp:Label><br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br /><br />

<asp:GridView ID="GridView1" runat="server" DataSource="<%# GetUploadList() %>" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Uploaded File">
<ItemStyle HorizontalAlign="Center" Width="70%" />
<ItemTemplate>
<asp:HyperLink
ID="FileLink"
NavigateUrl='<%# "media/ul/" + Container.DataItem.ToString() %>'
Text='<%# Container.DataItem.ToString() %>'
runat="server" Target="_blank" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemStyle HorizontalAlign="Center" Width="30%" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("Are you sure you want to delete this entry?");'
Text="Delete?" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The code-behind will look something like this:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

override protected void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
{
GridView1.DataBind();
}
}

override protected void OnInit(EventArgs e)
{
base.OnInit(e);

Button1.Click += Button1_Click;
}

protected string[] GetUploadList()
{
string folder = Server.MapPath("/media/ul");
string[] files = Directory.GetFiles(folder);
string[] fileNames = new string[files.Length];
Array.Sort(files);

for (int i = 0; i < files.Length; i++)
{
fileNames[i] = Path.GetFileName(files[i]);
}

return fileNames;
}

protected void UploadThisFile(FileUpload upload)
{
if (upload.HasFile)
{
string theFileName = Path.Combine(Server.MapPath("/media/ul"), upload.FileName);
upload.SaveAs(theFileName);
labelStatus.Text = "<b>File has been uploaded.</b>";
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
e.Cancel = true;
string fileName = ((HyperLink)GridView1.Rows[e.RowIndex].FindControl("FileLink")).Text;

fileName = Path.Combine(Server.MapPath("/media/ul"), fileName);
File.Delete(fileName);
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
UploadThisFile(FileUpload1);
GridView1.DataBind();
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment