One of the most common requests I hear is to provide an “easier-than-easy” way to make documents available on the web. Easy is a web form with a file upload control, so that won’t cut it. Easier-than-easy refers to the same drag-and-drop simplicity that users are accustomed to when sharing files over a network.
This sample application provides a web-based interface to a file system. It is coded in ASP .NET, and showcases the language’s ability to send files over HTTP with minimal code.
Starting with line 3, we point the application to the home directory. It makes sense to define a top-level folder in the application and work with paths relative to it. This is so that a user can’t pass “c:\” to the web application and get at system files; only resources within c:\webapps are available.
Lines 4 and 5 attempt to retrieve parameters from the HTTP request, identifying whether the user has clicked a link to a file or a subdirectory. If a file has been requested, lines 8 and 9 format the HTTP response so that it prompts the user with a download dialog. Line 11 is the method call that actually sends the file – Response.WriteFile() is a new feature in .NET that helps abstract away some of the complexities of file I/O.
If a file link wasn’t clicked, we check to see if a subdirectory link was clicked. If not, we print the listing of the base folder. If a subdirectory was clicked, we change the current folder and get its listing instead.
When looping through the directories and files to create links, we trim off the base folder part of the path using the substring method. Even if a user of this application views the page source, there will be no indication of where these files are actually housed on the server.
You need to be careful not to print any stray HTML tags when returning a file. Anything else written to the HTTP response can potentially corrupt the file’s contents. It’s a good idea to include lines such as 10 and 12 to flush the Response before writing the file and end it when the file is sent.
I also have a version of this application coded in Java, and would be happy to send the source to anyone who emails me for it.
3: Dim BaseFolder as String = “c:\\webshare”
4: Dim RequestedFile as String = Request.QueryString.get(“file”)
5: Dim CurrentFolder as String = Request.QueryString.get(“dir”)
6: if Not RequestedFile Is Nothing then
7: Dim file as new FileInfo(BaseFolder+”\”+RequestedFile)
8: Response.ContentType = “application/octet-stream”
9: Response.AddHeader(“Content-Disposition”,”attachment; filename=””” + file.Name + “”””) 10: Response.Flush()
14: if CurrentFolder Is Nothing then
15: CurrentFolder = BaseFolder
17: CurrentFolder = BaseFolder+”\”+CurrentFolder
18: end if
19: Dim dir As New DirectoryInfo(CurrentFolder)
20: Response.Write(““+ CurrentFolder.Substring(BaseFolder.length) +”\
24: Dim subdir as DirectoryInfo
25: For each subdir in dir.GetDirectories()
27: Next subdir
31: Dim file As FileInfo
32: For Each file In dir.GetFiles()
34: Next file