Advertisment

Website File Manager

author-image
PCQ Bureau
New Update

On the Internet, we often come across sites that provide free hosting services without the FTP facility. On such sites, file management is done through an interface that they provide, but which may or may not have all the features you need. In this article, we’ll see how can you use ASP.NET to implement a file manager on your website. 

Advertisment

Conventional ASP offered most of the features that were required for file management, but lacked in several areas (primarily due to corresponding absences in the language used). For example, a VBScript script could not read and output binary data without corruption creeping in. You had to often go through longwinded alternatives to get the work done. ASP.NET removes these problems with its amalgamation of features from all languages. There are also many classes and methods that you can call to make your work easier. There is also no need to ensure that particular third-party components are installed on the host server and that you have access to them. ASP.NET, by default, exposes all its assemblies to its scripts. This, by far, is the greatest advantage of coding in

ASP.NET.

Before we get down to implementing the file manager, let’s take a look at the basic operations that a file manager needs to perform. 

  • Get in and out of folders
  • Display the files and subfolders in each folder
  • Rename files and folders
  • Copy or move files and folders
  • Delete files and folders
  • Upload files
  • Create folders
Advertisment

Other than these seven, you can also add features to upload certain files to pre-designated folders, such as databases to a folder called ‘database’ or picture files to a folder named ‘images’. 

Remember that during every stage, you need to actively track which folder the user is in. We will use ‘/’ as the default the home folder (this is the root of the current ASP.NET application).

Initializing application



We will use WebMatrix (on this month’s PCQEssential CD) to create an application. First, create a folder for the Web application (say, C:\WMProjects\FileManager). Now, go to the folder where WebMatrix was installed, locate the file called webserver.exe there and create a shortcut to it on your desktop and rename the shortcut to ‘File manager’. In the Properties box of this shortcut, in the command line field, append /port:2003 /path:C:\WMProjects\FileManager. Start WebMatrix from the programs menu. In the Add New File dialog box, select (General)>ASP.NET Page. In the location field, enter C:\WMProjects\FileManager and in the filename box, enter

filemgr.aspx.

Advertisment

Designing page



Click on the Design tab at the bottom and from the toolbox’s Web Controls section, drag and drop the following controls and arrange them appropriately: one listbox (FolderContents), five buttons (CDIn, CDOut, Refresh, OpDo and Upload), one combobox (itemOp), one textbox (NewLocation), one label (lblStatus) and one file upload HTML control (HTML Elements, name it File1).

Coding



Enumerating the folder contents is easy. There’s the System.IO namespace that provides three very useful classes: Directory, File and Path. We use the Directory class’ GetDirectories method to retrieve the subfolders and the GetFiles method to get its files. These methods will return an array of name strings.

To get in and out of folders, we need to modify the tracked current path. This is easy using the Path class’ GetDirectoryName (to get the parent directory) and Combine (to add the current path and selected subfolder names), thus:

Advertisment

to get the parent folder path:

newPath = Path.GetDirectoryName(currentPath)

to get the sub-folder path:

Advertisment

newPath = Path.Combine(currentPath,

FolderContents.SelectedItem.Value)

Uploading files is still a bit of a chore, but easier than it used to be in traditional ASP. We have added only one file upload control in this sample, but you can easily add more (name them whatever you want) and still use the provided code without any modification! The Request.Files collection returns a set of HttpPostedFile items, each of which refer to one of the files that was sent to us. We can enumerate them using the For…Each construct and save them as we go along. You can also place the code to validate each file for size, type, etc, based on the restrictions you want to enforce. Here, you can also write code to transfer uploaded files to designated folders as suggested above. All of the file and folder operations described above use the three classes we described earlier–Path, Directory and File–encapsulated in the System.IO namespace. This makes our job very easy since we don’t have to use components external to the ASP .NET server system to do the same thing. To see the completed version, overwrite this file with the one on the CD and run that. 

Note that Win 2003 doesn’t allow transition into other virtual roots and application folders, as a security feature. This means that you cannot build a common file manager and share it between your various websites. You will have to copy these files into every website you want to use it for. Also, make sure that you do not set the folder with the file manager as an application root. This should reside in a subfolder of another application’s root.

Sujay V Sarma

Advertisment