![]() |
Web Development »
ASP.NET »
General
License: The Code Project Open License (CPOL)
Powerful File managerBy mohsen ahmadianA Powerful File manager in pure asp.net |
Javascript, CSS, HTML, XHTML, ASP, ASP.NET, WebForms, Ajax
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
In the web application some times you need to manage your files or allow your users to manage their files. In this time you need a file manager and creating a powerful solution is time consuming, but you don't have enough time. This was the story of my work when I needed it. I think if I write a powerful file manager that is simple to use and portable, did my best.
First step of any project get my requirements I need a file manager has this features:
- Create new directory
- Create new html/text file
- Edit html/text files
- Upload file
- Upload and unzip files in to destination directory
- Create Archive in server (in zip format)
- Show list of Files/Directory
- Show Attribute of Files
- Delete files
- Delete Directory
- Pure ASP.net (without any configuration)
- Skin able (change with CSS)
- User Friendly
- Security
- Easy to install
Using power file manager (in short PFM) is very simple to install. In first step copy files and directories in your web application then copy bin files to your bin directory next add link of PFM in your app.
Now your app has one PFM.
To change skin of PFM you have many customizations
PFM use MCE_tiny Editor to Create/Edit Html File. This is a powerful editor and you can change it with any Html Editor compatible with asp.net but this is not a limitation.
This is simple on PFM because PFM has simple structure and you can control it by few changes in code for examples if you need change user permission to avoid deleting file, you can hide delete file in command line and so one.
This is very simple but PFM on this Article doesn't have any control on user and its sample of PFM. For making it to your interest you should change it.
If you want to use it for multi users you have to change selection method. as a suggestion you can use cookie.
To create or Open Archive I use "ICSharpCode.SharpZipLib" Library. This API is cool and is simple to work with ZIP Archive (Under GNU).
My solution to show a list of files use an ASP Table, but if you know about asp table this is very simple .for this attention I wrote three classes to wrap and use easily
public class MyTableCell:TableCell //Describe new Table Cell
public class MyLinkButton:Label //Describe Link button (text + image)
public class MyImageButton:Label //Describe Image button
Now all things are ready to create a list of files and directories:
string p = Server.MapPath(startpath);
if (!Directory.Exists(p))
{
MultiView1.SetActiveView(View2);
return;
}
string[] fns = Directory.GetFiles(p);
string[] dns = Directory.GetDirectories(p);
string uplevel = startpath;
if (uplevel.LastIndexOf("\\") > 0)
uplevel = uplevel.Substring(0, uplevel.LastIndexOf("\\"));
Table1.Rows.Clear();
Table1.CssClass = "ms-list8-main";
TableRow tr = new TableRow();
MyTableCell head = new MyTableCell("ms-list8-top", new MyImageButton("softimages/folder_home.png", "Default.aspx?goto=listview", "Root")
, new MyImageButton("softimages/back.png", "Default.aspx?goto=listview&start=" + uplevel, "Back")
, new MyImageButton("softimages/top.png", "Default.aspx?goto=Upload&start=" + startpath, "Upload File")
, new MyImageButton("softimages/newfolder.png", "Default.aspx?goto=newFolder&start=" + startpath, "New Folder")
, new MyImageButton("softimages/mimetype/html.png", "Default.aspx?goto=addnew2&start=" + startpath, "New Text File")
, new MyImageButton("softimages/ark.png", "Default.aspx?goto=zip&start=" + startpath, "Archive")
, new MyImageButton("softimages/reloadark.png", "Default.aspx?goto=ReloadZip&start=" + startpath, "Clear Archive")
, new MyImageButton("softimages/delark.png", "Default.aspx?goto=DeletArch&start=" + startpath, "Delete Archive"));
head.ColumnSpan = 5;
tr.Cells.Add(head);
Table1.Rows.Add(tr);
tr = new TableRow();
tr.Cells.AddRange(new TableCell[] {new MyTableCell("File Name","ms-list8-tl"),new MyTableCell("Size","ms-list8-top")
,new MyTableCell("Creation Time","ms-list8-top"),
new MyTableCell("Last write time","ms-list8-top") ,new MyTableCell("Commands","ms-list8-top")});
Table1.Rows.Add(tr);
PrintList(dns, false);
PrintList(fns, true);
tr = new TableRow();
MyTableCell mtc = new MyTableCell(" ", "ms-list8-bottom");
mtc.ColumnSpan = 5;
tr.Cells.Add(mtc);
Table1.Rows.Add(tr);
MultiView1.SetActiveView(View2);
At first we clear all table cells
Table1.Rows.Clear();
Table1.CssClass = "ms-list8-main";
Now create new Table row
TableRow tr = new TableRow();
So, create new Table Cells for command button bar
MyTableCell head = new MyTableCell("ms-list8-top", new MyImageButton("softimages/folder_home.png",
"Default.aspx?goto=listview", "Root"), new MyImageButton("softimages/back.png",
"Default.aspx?goto=listview&start=" + uplevel, "Back"),new MyImageButton("softimages/top.png",
"Default.aspx?goto=Upload&start=" + startpath, "Upload File"),
new MyImageButton("softimages/newfolder.png", "Default.aspx?goto=newFolder&start=" +
startpath,"New Folder"), new MyImageButton("softimages/mimetype/html.png",
"Default.aspx?goto=addnew2&start=" + startpath, "New Text File")
, new MyImageButton("softimages/ark.png", "Default.aspx?goto=zip&start=" + startpath, "Archive")
, new MyImageButton("softimages/reloadark.png", "Default.aspx?goto=ReloadZip&start=" +
startpath, "Clear Archive"), new MyImageButton("softimages/delark.png",
"Default.aspx?goto=DeletArch&start=" + startpath, "Delete Archive"));
head.ColumnSpan = 5;
Set column span to 5 to balance the table. After this add head to table row and table
tr.Cells.Add(head);
Table1.Rows.Add(tr);
Now we must create new row for title of each cells
tr = new TableRow();
tr.Cells.AddRange(new TableCell[] {new MyTableCell("File Name","ms-list8-tl"),new
MyTableCell("Size","ms-list8-top"),new MyTableCell("Creation Time",
"ms-list8-top"),new MyTableCell("Last write time","ms-list8-top")
,new MyTableCell("Commands","ms-list8-top")});
Table1.Rows.Add(tr);
In this time we have Table with command bar and title bar and can add list of directory's and files
PrintList(dns, false); // dns is list of directories name
PrintList(fns, true); // fns is list of files name
Print list is a procedure with two parameters to show list in table
private void PrintList(string[] fns, bool isfiles)
Other source code of create list is very easy and don't need any more comment.
In this project we have many pages to create, delete, rename and etc….
On the other hand we must create project in one page because it must be portable, in this case my idea to solve this problem is using Multiview solution.
Multiview is an ASP.net tools to create many pages in one page.
How to handle views or user commands, when we use multi view?
To answer this question we have two choices
Event handling Or Query Handling (QH)
Event Handling is not very flexible on the other hand QH is very flexible.
QH is an old, but very powerful solution.
So, answer is QH.
if (!IsPostBack)
{
string w = Request["goto"];
if (w == null)
ViewList();
else
{
switch (w)
{
case "addnew":
MultiView1.SetActiveView(View1);
Label4.Text = "Create Text File";
TextBox1.ReadOnly = false;
Button7.Visible = false;
break;
case "addnew2":
MultiView1.SetActiveView(View1);
Label4.Text = "Create Text File";
TextBox1.ReadOnly = false;
Button7.Visible = true;
break;
case "listview":
ViewList();
break;
case "edit":
editThisfile();
break;
case "ren":
renamethisfile();
break;
case "del":
deleteThisfile();
break;
case "info":
information();
break;
case "link":
showlink();
break;
case "newFolder":
MultiView1.SetActiveView(View7);
break;
case "Upload":
MultiView1.SetActiveView(View8);
break;
case "zip":
CreateZip();
break;
case "AddZip":
AddtoZip();
break;
case "RemoveZip":
RemoveZip();
break;
case "ReloadZip":
ReloadZip();
break;
case "DeletArch":
DeletArch();
break;
default:
break;
}
}
}
If you have a great project you will have huge errors! To solve this problem you must monitoring your errors and save them when users work with your application. Dr. Watson was a person who wrote any events. So now we have a class with this title.
DrWatson (our class) just writes thrown exceptions in a text file called "DrWatson.txt" with date/time and source of throw.
catch (Exception ex)
{
DrWatson.SaveError(ex);
}
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Jun 2008 Editor: |
Copyright 2008 by mohsen ahmadian Everything else Copyright © CodeProject, 1999-2010 Web20 | Advertise on the Code Project |