Sunday, July 6, 2008

File upload manager Web User Control in ASP.net


Here is some code for a File upload manager control.
This control can:


  • Upload multiple files

  • Remove any uploaded files

  • Check for duplicate uploads

  • Reduces risk of concurrent file access issues by assigning a sessionId sub-folder under the upload directory.

  • Remembers uploaded files over post backs.

All you have to do is assign the FileUploadDirectory property. This will be a full local path on the local machine.


Note. I have written this control with the intention of creating a re-usable dll, to see how to do this check my blog here: http://maoriitguy.blogspot.com/2008/07/creating-ascx-library.html


Create a new file in VS named FileUploadManager.ascx and paste this code:



<%@ Control Language="C#" ClassName="DKCS.FileUploadManager" %>
<script runat="server"> private string _fileUploadDirectory;
public ListItemCollection UploadFiles { get { return CheckBoxList1.Items; } }
public string Title { get { return Label1.Text; } set { Label1.Text = value; } }
public string FileUploadDirectory { get { return HiddenFieldUploadDir.Value; } set { if (!System.IO.Directory.Exists(value)) ThrowNotImplementedException();
_fileUploadDirectory = value + "/" + Session.SessionID;
try { System.IO.Directory.CreateDirectory(_fileUploadDirectory); } catch { ThrowNotImplementedException(); }
HiddenFieldUploadDir.Value = _fileUploadDirectory; } }
protected void Button2_Click(object sender, EventArgs e) { if (FileUpload1.FileName == null FileUpload1.FileName.Equals("")) return;
if (FileUploadDirectory == null FileUploadDirectory.Equals("")) { ThrowNotImplementedException(); }
//check File exists foreach (ListItem item in CheckBoxList1.Items) { if (item.Text.Equals(FileUpload1.FileName)) return; } string localFilePath = FileUploadDirectory + "/" + FileUpload1.FileName; FileUpload1.SaveAs(localFilePath);
CheckBoxList1.Items.Add(new ListItem(FileUpload1.FileName, localFilePath)); Button1.Enabled = true; }
private void ThrowNotImplementedException() { throw new NotImplementedException("The FileUploadManager.FileUploadDirectory property must be set to a absolute directory path on the local server. Ensure the asp.net application has permissions to read and write to the upload directory."); }
protected void Button1_Click(object sender, EventArgs e) { ListItemCollection temp = new ListItemCollection();
foreach (ListItem item in CheckBoxList1.Items) { if (item.Selected) { System.IO.File.Delete(item.Value); temp.Add(item); } }
foreach (ListItem item in temp) { CheckBoxList1.Items.Remove(item); }
if (CheckBoxList1.Items.Count == 0) { Button1.Enabled = false; } }
</script>
<div> <table> <tr> <td align="center" colspan="2"> <asp:Label ID="Label1" runat="server" Text="Upload Files"></asp:Label></td> </tr> <tr> <td> <asp:FileUpload ID="FileUpload1" runat="server" Width="300px" Height="24px" /></td> <td> <asp:Button ID="Button2" runat="server" Text="Upload" OnClick="Button2_Click" Width="80px" /></td> </tr> <tr> <td colspan="2"> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> </asp:CheckBoxList> <asp:HiddenField ID="HiddenFieldUploadDir" runat="server" /> </td> </tr> <tr> <td align="center" colspan="2"> <asp:Button ID="Button1" runat="server" Text="Remove Selected" Enabled="False" OnClick="Button1_Click" /></td> </tr> </table></div>




Here is a screen of it inside a WebPartZone:


No comments: