A tutorial for those who want to learn HTML in a quick and easy way. Web File Manager - Code. Project. A single page ASP. NET file management utility. Introduction. I often deploy ASP.
Configuration Tasks. This scenario demonstrates file management on Cisco Catalyst switches. In this section you learn how to perform the following configuration tasks: Transferring Files using TFTP; Upgrading/Rolling Back the. 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 Web. File. Manager page than can be dropped into any ASP. NET website. This page performs the most common file and folder operations: Uploading Deleting Renaming Copying Zipping Moving Adding Web. File. Manager to an existing ASP. NET project is relatively straightforward. It can be done in one of two ways: Deployment as Inline Code. The first method, and I think the easiest, is to deploy the inline code version of Web. File. Manager: default- inline. Make a copy of this file and rename it default. Copy the Web. File. Manager folder to your web server. This folder only needs the following files: \Web. File. Manager\default. Web. File. Manager\images\file\*. Web. File. Manager\images\icon\*. And you're done! The main advantage of this approach is that it doesn't require any changes to Web. 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- Behind. If you're more comfortable with a typical code- behind page, that can also be deployed relatively easily: default. Web. File. Manager. Copy the Web. File. Manager folder to your web server. This folder only needs the following files. Web. File. Manager\default. Web. File. Manager\bin\Web. File. Manager. dll\Web. File. Manager\images\file\*. Web. File. Manager\images\icon\*. Because code- behind generates a separate assembly DLL, you have to let the main webapp know where to find this file. Modify Web. config as follows. Bindingxmlns="urn: schemas- microsoft- com: asm. Path="http: //www. Web. File. Manager/bin"/> < /assembly. Binding> < /runtime> The page should now load using all default settings. If you want to override the defaults, see the configuration table later in this article for additional Web. Whichever method of deployment you use, the new ZIP functionality requires ICSharp. Code. Sharp. Zip. Lib. 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 for. Bear 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 \Web. File. Manager\Web. If you want file operations to occur as a specific user: enable impersonation. If you want to restrict access: set authorization. Don't forget that web. Implementation. I'll warn you up front: this page is not a model of proper ASP. NET design. It uses Response. Write extensively, and does not use a single server control. I kept it deliberately old school, because I wanted a minimal amount of code and tight control of the HTML produced. I don't recommend this approach for a larger project, but I think it's OK for a single utility page. As for the code, there is a handful of static HTML in default. The static HTML contains some Java. Script and a basic stylesheet, as well as the HTML page template. The page body, however, is rendered by the Write. Table method. The main loop is very straightforward: it iterates through every directory and file in the current directory, and copies that to a simple Data. Table structure, which is then sorted into a Data. View and dumped to the output buffer in a simplified table format. The table is designed to render progressively using the "table- layout: fixed" style attribute: With Response. Write("< TR> "). Write("< TD align=right> < INPUT name="""). Write(_str. Checkbox. Tag). . Write(str. File. Name). . Write(""" type=checkbox> "). Write("< TD align=center> < IMG src="""). Write(File. Icon. Lookup(drv)). . Write(""" "). Write(_str. Icon. Size). . Write("> "). Write("< TD> "). Write(str. File. Link). Write("< TD align=right> "). If bln. Folder Then. Write("< TD align=left> "). Write(Format. KB(Convert. To. Int. 64(drv. Item("Size")))). Write("< TD align=left> kb"). Write("< TD align=right> "). Write(Convert. To. String(drv. Item("Created"))). Write("< TD align=right> "). Write(Convert. To. String(drv. Item("Modified"))). Write("< TD align=right> "). Write(Convert. To. String(drv. Item("Attr"))). Write(Environment. New. Line). Flush()File and folder operations are triggered by the checkboxes next to each row and a hidden form field with an action string. The Java. Script functions do some basic client- side error checking, set the action form field appropriately, and submit the form. On postback, the Handle. Action method is called: Public. Sub Handle. Action(). If Request. Form(_str. Action. Tag) Is. Nothing. Then. Return. Dim str. Action As. String = Request. Form(_str. Action. Tag). To. Lower. If str. Action = ""Then. Return. Select. Case str. Action. Case"newfolder". Make. Folder(Get. Target. Path). Case"upload". Save. Uploaded. File(). Case. Else. Process. Checked. Files(str. Action). End. Select. If. Not _File. Operation. Exception Is. Nothing. Then. Write. Error(_File. Operation. Exception). End. If. End. Sub. I am capturing any file operation failures into a class level variable _File. Operation. Exception. If a file delete fails because the ASPNET process doesn't have access to it, we just want to display a simple message- - not throw a massive exception all the way up to the main application. Configuration. This page does have a few configuration options it looks for in Web. These are all optional.< addkey="Web. File. Manager/Image. Path"value= "resources/Web. File. Manager/images/"/> < addkey="Web. File. Manager/Hide. Folder. Pattern"value= "^bin|test"/> < addkey="Web. File. Manager/Hide. File. Pattern"value= "scc$"/> < addkey="Web. File. Manager/Allowed. Path. Pattern"value= "/My. Web/Uploads/.*"/> < addkey="Web. File. Manager/Default. Path"value= "~/My. Web/Uploads"/> Property. Default. Description. Image. Path"images/"Root- relative path to the file/*. Hide. Folder. Pattern""Folder names matching this regular expression will not be displayed. Hide. File. Pattern""File names matching this regular expression will not be displayed. Allowed. Path. Pattern""The user will only be allowed to navigate to paths that match this pattern. Default. Path"~/"Default path that will be displayed if no path is specified in the path= Query. String. Flush. Content. False. Issue a Response. Flush after writing each table row. Renders faster, but some Http. Modules don't work with partial content. Conclusion. Web. File. Manager 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. Friday, November 2. Wednesday, January 5, 2. Version 1. 1. Added persistent up/down column sorting by clicking column headers Default sort is now by name instead of "as returned by . NET functions" Added ability to ZIP files using Sharp. Zip. Lib. Improved Fire. Fox support (Fire. Fox doesn't support < COLGROUP> alignments?!) Move and Copy now create destination folders if it doesn't exist. Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2016
Categories |