简单的Web形式文件管理器_第1页
简单的Web形式文件管理器_第2页
简单的Web形式文件管理器_第3页
简单的Web形式文件管理器_第4页
简单的Web形式文件管理器_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、简单的Web形式文件管理器简单的Web形式文件管理器Before:This article is acodeprject best article of March 2010,you could find all the mothly best articles here:Introduction This article,aimed at the beginner level,describes asimple web that administers the files and folders on ahosted web site where,for instance,t

2、he web host does not provide a utility and you do not have access to any other means of managing or administering those files and folders.The objective is to create asmall,simple application that employs as few objects as possible so that it is easy to deploy with no reliance on any other resources

3、such as images or style sheets as these have been embedded directly into the assembly.Caveat 1:The article does not cover the security with which you should surround this and any other utilities that you use to administer your web site.Caveat 2:I have tested WebFiler on anumber of hosted web sites t

4、hat have no more than 50 folders/sub-folders and less than athousand files and im ages.Whilst,in theory,it should work without ahitch on any size web site,if your web site is much larger than this it is probable that acommercial application would serve you better.Test thoroughly before final deploym

5、ent and use.Background Im sure that,like most people reading Asp.Net articles,you have,at one time or another,created aweb site which you are expected to administer;either in terms of adding content(not covered here)or by making changes to physical files and having to upload/download them from the s

6、ite as well as maintaining the folder structure.Many web hosts will offer arudimentary utility for your web site but its normally up to you to implement your own solution.There are anumber of approaches to this.You could use an such as could use afree application likeWebApplication Lite(though that

7、is aclassic asp application)or you could buy acommercial application like subject has also been covered here in the past and Ileave you free to wander the hallowed halls ofCodeProjectto judge those articles for yourself.The difference between WebApplication Lite and is that the latter is the more mo

8、dern of the 2(Asp.Net/Ajax vAsp Classic)with far more features including adrop in control version.However,it is not afree product.WebApplication Lite is free but is an ASP Classic application wit ha large footprint(many files and folders).You have probably had to use amixture of the web hosts own ut

9、ilities and/or tools like WebApplication Lite simply because it is adrop in application and takes zero maintenance.However,there are times(holiday/vacation/new client site)when you may not have reasonable access to (or similar applications)or might not have one of the many passwords with you that yo

10、u would need to access the web hostsutility directly.WebFiler attempts to address that with asimple http solution that has asmall footprint and is simple to deploy with minimal configuration.Requirements First and foremost the solution has to be simple with as few files and dependencies as possible;

11、that meant embedding any images,etc.Whilst this does have aslight performance impact it is negligible:there are 5small images and a13 line stylesheet-these will not break the bank.This could have been done with just one aspx page but Idecided that it would be easier to maintain and understand as 2pa

12、ges:one to display the files and folders,the other to carry out simple tasks such as renaming files or uploading new files.The application consists of 4objects:(optional)and Web only other task for users would,optionally,be to change avalue in the root Web.config but more on that later.Using the cod

13、e As mentioned above this asimple application predicated on obtaining alist of files and folders on the server and presenting them,with various options,in as simple amanner as possible.Web.config Firstly,lets look at the changes that are required to be made in the config are 2ways that this can be a

14、ccomplished:Make changes to the root level web.configAdd anew Web.config in the same folder that you put and m going to presume that you will not put these files in your root folder.You can if you want to,I cant stop you but unless you can ensure that no one will find and abuse them Iwould put them

15、into afolder that already contains management utilities(Maybe like root/Admin/.)or create anew folder especially for the purpose and then protect that folder.As Isaid before,how you handle your security is outside the scope of this article;suffice to say that Iset folder level security so that Iam a

16、sked for auser name and password to access any files in that directory.My recommendation is that you put the files into asub folder calledAdmin.You can either drop the supplied Web.Config in here or modify the root Web.Cofig with these 2entries.CollapseCopy Code?xml version=1.0?configuration appSett

17、ings add key=WFRootvalue=./appSettings system.web httpRuntime maxRequestLength=2097151/system.web/configurationWFRoot:This tells WebFiler where the root of your application is.If you leave it empty the folder where you launch from will become the root and you will not be able to navigate out past it

18、.If you only want to see,for instance,the Images folder below the root then change it to./Images.Naturally,you will need to play around with this to give you what you want.However,if you simply create afolder called Admin or Bob or WebFiler and put the files in here then leave as is and you should h

19、ave access to all of the files and folders in your application.Note:You will not be able to see files and folders that have an attribute of Hidden and/or System but you can see and manipulate files and folders that are marked as ReadOnly.maxRequestLength:This is the setting that restricts the size o

20、f files you can upload.The default setting is 4096.Fi ne if all you ever upload are small files of less than 4MB.I have set it here to 2097151-the maximum.That is insanely high.Dont leave it at that unless you have areally,really good reason to do so and really,really need to upload 2gig files.Note:

21、Visual Studio does not include this setting by default when you create anew web application so you may need to add it if you hadnt already done so.You can also put WFRoot in the root level Web.Config and add/change the maxRequestLength value in the same way which means that you can dispense with pla

22、cing the Web.config in the same folder as and cuts the object load down to 3files.Web This is the compiled code that underlies the must be placed in the bin folder that should exist below your root folder which is where it is expected to be found.If it is not found here you will get aParser Error,Co

23、uld not load type Web-the page cannot find the compiled code behind(Web ll)and will,therefore,generate anerror.It also contains all of the required images and styles thus reducing the footprint of folders and files that have to be deployed.The content is discussed below. This page displays atable of

24、 the files and folders and allows the user to navigate between folders and instigate related functionality such as uploading anew file. This page handles anumber of events on behalf of and uses aMultiView control to display each of those functions.TableEx.cs This class contains the code to create th

25、e table of files and folders that you first see displayed.I have implemented it use plain html tables since they are simple to use and produce excellent tabular displays.I could have passed everything into,for instance,a DataTable but since aDataTable gets rendered as an html table it would have add

26、ed complexity rather than simplifying the process.The most important methid in this class is getting alist of files and folders to display.Each time you navigate to adifferent sub-folder or back to the root level the class is called and anew table constructed.The listings are obtained as string arra

27、ys,one for the folders and one for the files and both are then copied to acombined array,folders first so that they appear before the files.CollapseCopy Codestring (_root);string folders=Directory.GetDirectories(_root);_data=new string;folders.CopyTo(_data,0);(_data,folders.Length);The table is then

28、 constructed by first building abasic table and adding aheader structure that contains the various columns for display.Arootrow is also,if appropriate,created.In other words,if the present view isnt the topmost view(i.e.are we in the root folder?)add arow that simply allows us to navigate back up on

29、e folder.Wa also implement aHomebutton which will return directly to therootfolder regardless of current folder position.Of course theHomebutton is not rendered if we are in the root since there is no need for it.(Shown in the image above for illustrative purposes)The next task is to create arow for

30、 each folder and this is accomplished in the DataRows method.Since we now have the data in_data(string)we can use aforeach to find each row and then act on whatever we find.CollapseCopy Codeforeach(string item in _data)The big decision here is are we allowed to see what we find?The easiest way to de

31、termine this is to create a object fromitemand examine it thusly:CollapseCopy Code/If the item attributes are Hidden or System,ignore.if(fi.Attributes&)=|(fi.Attributes&)=)continue;This just says if the objects attributes are System or Hidden exit the loop at this point and come back in from the top

32、.Next,we have to decide if we are looking at a afolder.Again,this is straightforward:CollapseCopy Codeif(fi.Attributes=)If the item is afolder(directory)then we would treat it alittle differently from a that we simply want to navigate to the folder whereas we want to open or download afile.Embedded

33、Resources This seems like agood time for aquick diversion.Weve talked about embedding objects into the assembly and this is one of the places where we retrieve and use aresource for these items.In this case it is an image denoting either a afolder.At the solution level images are stored within the I

34、mages folder.However,if you select an image and right-click to view the properties you will see that theBuild Actionhas been set to embedded;in other words embed acopy of this image into the compiled assembly.At first sight this is simple enough;however,how can you actually use this image with,for i

35、nstance,another object that requires aphysical location for the object so as to display it?In this instance we want to use this image as the source for aSystem.Web.UI.WebControls.Image which needs an ImageUrl.The answer is in 2parts.Firstly you have to tell the application about this object.You do s

36、o through the AssemblyInfo.cs so:CollapseCopy Codeassembly:WebResource(Web,img/png)This breaks down as Solution.Location.Object,object type.For the style sheet it is very similar:CollapseCopy Codeassembly:WebResource(Web,text/css)To use this as aurl you need the following:CollapseCopy Code/Get the f

37、older image.System.Web.UI.WebControls.Image img=new System.Web.UI.WebControls.Image();img.ImageUrl=Page.ClientScript.GetWebResourceUrl(typeof(Web),Web);You need to tell themethodthe type of the resource and the name of the resource.In this instance we have passed the assembly as the type and the loc

38、ation of the it exists within the solution.The application then translates this for the image and uses WebReource.axd to supply auseable ImageUrl.Result?No need to deploy the phyical images.However,be warned,this is okay for afew images and small objects;probably not so good for large numbers of ima

39、ges,etc.Use your own judgement and test,test,test.Back at row creation.Back at row creation we would also use to grab some other properties of the folder for display.Finally,we have to add acouple of buttons so that objects can be renamed or deleted.The rename simply opens using aquerystring(more on

40、 that later)to tell the page what we want to do.In the case of the delete event we have to be alittle more careful since the method will fail when trying to delete readonly we display the readonly status of files it is fairly easy to see that we probbaly shouldnt delete it otherwise why is it set to

41、 read only?This is further complicated when attempting to delete afolder and all of its sub-folders.If any object in that list is read only the process will generate an exception.Not the desired behaviour.The way Ihave chosen to deal with this is to unset the readonly attribute.For files this is eas

42、y:CollapseCopy Code fi=new (file);fi.Attributes=;(file);However,it is somewhat more convoluted for folders;the idea is to create acollection of folders(and files)and reset the attributes as each is found using DirectoryInfo.At the same time,unset any files found within that folder.The method can be

43、seen at line 504,ReadOnlyFolderDelete.The core is to reset each folder then reset all the files within that folder.Once all the folders and files have been unset call the Delete method to recursively delete all of the folders.Request.QueryString There are anumber of different ways to pass data betwe

44、en forms;Session,Hidden Fields,etc;but Idecided to use QueryString since this is asecure,administrator only application.I have also encoded(UrlEncode/UrlDecode)the strings that represent files and folders so as to circumvent any potential problems with strings that contain characters that could,pote

45、ntially,cause problems.All this means is that,for instance,a space(character code 32)is tranlated to aplus sign(+)and an ampersand(&)to%26.Opening Files My first cut of this was alittlefancierin that Ihad amethod that deduced the mime type and passed this as the content-type.I also had graphics that

46、 represented each one.I quickly realized that this was not the way to go.Firstly,at 16x16 it is hard to see the differences between each of the images that represent and went against the underlying principle of keeping it simple.It would also have meant embedding large numbers of image files into th

47、e assembly:another no-no.Most importantly,its really not necessary.Using acontent type ofapplication/octet-streamcovered every case Icould find(not exhaustive,by any means)which cuts down on code and resources.This code passes the the Response object with the content type,creates anattachmentand use

48、sResponse.TransmitFileto display the .CollapseCopy Code fi=new (file);Response.ContentType=application/octet-stream;Response.AppendHeader(Content-Disposition,attachment;+fi.Name);Response.Transmit);Response.End(); As mentioned above this page takes care of various general events on behalf of and use

49、s aQueryString to pass instructions and data.Part of the query string that gets passed containsmfor mode.This is parsed in the OnLoad event and the appropriate view is displayed by the simple expedient of matching the value ofmto the index of the views.CollapseCopy CodeMultiView.ActiveViewIndex=m;/m

50、odeThe events are driven by buttons(see main image above)on as follows:Rename In fact this is atable event(in that it is invoked for any row in the table of files and folders)but Iprefer to display aseparate page to handle this event as it is easier to maintain than crowding seemingly unrelated functionality onto one page and is more closely related to the other non-table based functions.Partial classes might have worked in this case but Iwanted to keep the rename controls away from

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论