|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI often deploy ASP.NET websites to servers that I don't control. In these situations, I can't get to the underlying file system to do any file maintenance, because I don't have direct access to the server. So I have to access the file system indirectly, through the website that I am deploying. Rather than writing a bunch of special purpose pages to deal with file management, I developed a generic WebFileManager page than can be dropped into any ASP.NET website. This page performs the most common file and folder operations:
Adding WebFileManager to an existing ASP.NET project is relatively straightforward. It can be done in one of two ways: Deployment as Inline CodeThe first method, and I think the easiest, is to deploy the inline code version of WebFileManager: default-inline.aspx. Make a copy of this file and rename it default.aspx. Copy the WebFileManager folder to your web server. This folder only needs the following files:
And you're done! The main advantage of this approach is that it doesn't require any changes to Web.config and thus doesn't force the app to restart. The downside is that the inline version of the page must be converted from the code-behind master; inline pages are a pain to debug and maintain. Deployment as Code-BehindIf you're more comfortable with a typical code-behind page, that can also be deployed relatively easily: default.aspx is the master, code-behind version of WebFileManager.
Whichever method of deployment you use, the new ZIP functionality requires ICSharpCode.SharpZipLib.dll to be present somewhere. I find that most of my web projects have this dependency already, but your mileage may vary. If you don't want this dependency, you'll need to comment out the ZIP code in the source. It's all in one function, so it isn't hard to remove. Things to watch out forBear in mind that file system operations will occur as the ASP.NET process account by default (machinename\ASPNET), not as the user accessing the page! Any permission errors during a file operation will be trapped and echoed at the top of the page. So if you're wondering "Why can't I upload a file?" or "Why can't I delete that annoying folder?", it's typically because the ASPNET account doesn't have enough filesystem permissions to do so. It's not included in the sample project, but there are a couple of reasons why you may also want to create a custom, local \WebFileManager\Web.config file:
Don't forget that web.config settings work fine on a per-folder basis, which is ideal for this purpose-- you can grant access to only a few users, or have the entire page run as administrator, without affecting the rest of the website. ImplementationI'll warn you up front: this page is not a model of proper ASP.NET design. It uses As for the code, there is a handful of static HTML in default.aspx, and an associated code-behind class. The static HTML contains some JavaScript and a basic stylesheet, as well as the HTML page template. The page body, however, is rendered by the The main loop is very straightforward: it iterates through every directory and file in the current directory, and copies that to a simple With Response
.Write("<TR>")
.Write("<TD align=right><INPUT name=""")
.Write(_strCheckboxTag)
.Write(strFileName)
.Write(""" type=checkbox>")
.Write("<TD align=center><IMG src=""")
.Write(FileIconLookup(drv))
.Write(""" ")
.Write(_strIconSize)
.Write(">")
.Write("<TD>")
.Write(strFileLink)
.Write("<TD align=right>")
If blnFolder Then
.Write("<TD align=left>")
Else
.Write(FormatKB(Convert.ToInt64(drv.Item("Size"))))
.Write("<TD align=left>kb")
End If
.Write("<TD align=right>")
.Write(Convert.ToString(drv.Item("Created")))
.Write("<TD align=right>")
.Write(Convert.ToString(drv.Item("Modified")))
.Write("<TD align=right>")
.Write(Convert.ToString(drv.Item("Attr")))
.Write(Environment.NewLine)
End With
Flush()
File and folder operations are triggered by the checkboxes next to each row and a hidden form field with an action string. The JavaScript functions do some basic client-side error checking, set the action form field appropriately, and submit the form. On postback, the Public Sub HandleAction()
If Request.Form(_strActionTag) Is Nothing Then Return
Dim strAction As String = Request.Form(_strActionTag).ToLower
If strAction = "" Then Return
Select Case strAction
Case "newfolder"
MakeFolder(GetTargetPath)
Case "upload"
SaveUploadedFile()
Case Else
ProcessCheckedFiles(strAction)
End Select
If Not _FileOperationException Is Nothing Then
WriteError(_FileOperationException)
End If
End Sub
I am capturing any file operation failures into a class level variable ConfigurationThis page does have a few configuration options it looks for in Web.config. These are all optional. <add key="WebFileManager/ImagePath"
value= "resources/WebFileManager/images/" />
<add key="WebFileManager/HideFolderPattern"
value= "^bin|test" />
<add key="WebFileManager/HideFilePattern"
value= "scc$" />
<add key="WebFileManager/AllowedPathPattern"
value= "/MyWeb/Uploads/.*" />
<add key="WebFileManager/DefaultPath"
value= "~/MyWeb/Uploads" />
ConclusionWebFileManager is a simple page, but it has worked well for me in a number of projects. Hopefully, it'll work for you too. There are many more details and comments in the demonstration solution provided at the top of the article, so check it out. And please don't hesitate to provide feedback, good or bad! I hope you enjoyed this article. If you did, you may also like my other articles as well. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||