ASP.NET+XML to build network hard disk principle analysis

  • 2020-05-17 05:17:57
  • OfStack

Common modes of transport and their difficulties in specific environments

We often need to exchange public data files over the network to share resources, protect private data from illegal access, and operate in a simple and intuitive way. We commonly used file transfer FTP, Email mail, online neighbors can achieve the transfer of files. Among them, "Ftp" has the most powerful function, but it is slightly complicated to use. 1. A lot of Settings are enough to make many people stop, especially when the number of users is unpredictable, the Settings for users with special needs will be more complicated. "Email" is familiar to everyone, but its transmission not only requires you to connect to Internet, and its security is also a problem, in the enterprise, financial, labor and other documents need to communicate through this way is not a good way; Online neighbors "is a kind of method of file transfer, through designated Web Shared folder and placed in the unit resource file, 1 set within the scope of the users can access to these files, the scope of using this way is quite limited, however, usually with 1 DNS segment address of the user can access, other especially Internet users, it is difficult to use, in addition with Email similar, its use is not intuitive, a lot of times you have to list in many computer 1 layer upon layer is it possible to get what you want to search resources!

Although it seems that the above three ways in 1 degree, has realized the file data communication, but they are mainly for transfer of a "peer-to-peer" (you can only passively wait for the other data placement and not take the initiative to "visualization" to ask for), unable to realize "1 piece of space, resources efficacy" of application requirements, this way of sharing based on "point to" need to seek additional transmission path, the network hard disk is a kind of very good solution.

Advantages of network hard disk

A network hard disk (also known as Shared space) is a piece of hard disk space on a server where, if you have enough permissions, you can do whatever you want with it, just like you would with your local computer. You know, this one is transmitted in the form of Http, presented in the form of Web in front of all users, through the browser such a familiar way of access, so that the "one piece of space, resources can see each other" sharing mode is realized!

In addition, the network hard disk interface on the client side, it allows the user to submit the data to the server, and then manage these information in the specified scope, is widely used in the processing pattern in actual development, especially in the aspect of building interactive web document management system, online examination system, the file transfer system, the news release system and company's bidding system and so on a large number of application!

The following netdisk systems were developed using Asp.net (Visual C#) and Xml (extensible markup language). The development details of the program and the key techniques and difficulties used in the code are explained in detail, with the help of these instructions, you can fully develop your own network hard drive.

Network hard disk to achieve the function

Our network hard disk realizes the following functions:

1. View the contents of the folder

2. You can set access rights when creating a new folder

3. Jump between folders: enter the next level and return to the previous level

4. Upload the file to the specified folder

5. Download files to the local machine or view the contents of files online

6. Delete files or folders

Development details and key technical points

1. View folder contents:

Program 1 will start by entering the specified root folder (e.g., \NetHard). The contents of this folder will be listed by the data bound control (DataGrid), including the following: category (distinguish folder or file), name, permission (is folder allowed? Can I download the file? , delete (are files or folders allowed to be deleted?) And for the creator of the folder. Here, the dynamic change folder content (you don't know when a user transmits the file or a folder or delete their new), we through dynamically created data table DataTable to read the contents of the specified folder, and then as a data source and display control DataGrid binding, in this way, data display is always timely. The function to create the dynamic table is as follows:
 
public DataTable Bind(string fullFolderPath)// Create a data table to read the contents of the folder  
{ 
// Here's how to create a data table dynamically  
DataTable dt=new DataTable(); 
DataRow dr; 
// Start by creating the data table structure  
dt.Columns.Add(new DataColumn("type",System.Type.GetType("System.String")));// type  
dt.Columns.Add(new DataColumn("name",System.Type.GetType("System.String")));// The name of the  
dt.Columns.Add(new DataColumn("action",System.Type.GetType("System.String")));// operation  
dt.Columns.Add(new DataColumn("owner",System.Type.GetType("System.String")));// founder  
// For each 1 Row fill data  
foreach(string d in Directory.GetFileSystemEntries(fullFolderPath)){ 
dr=dt.NewRow();// A new line  
string[] parts=d.Split(new char[]{'\\'}); 
string txt=parts[parts.Length-1];// Take the final 1 Part of a string that will likely be a file name or folder  
dr[1]=txt;// The name of the name 
if(File.Exists(d))// If it's a file  
{ 
dr[0]=" file ";// type type 
int pos=currFullPath.IndexOf("NetHard"); 
string relaUrl=currFullPath.Substring(pos); 
string 
url="http://10.80.50.1/SharedSpace/"+relaUrl+"/"+txt;// 
10.80.50.1 Is the author server address, you should change it to your server address  
dr[2]="<a href="+url+" target=_blank"+"> download </a>"; 
} 
else if(Directory.Exists(d)){// If it's a folder  
dr[0]=" folder ";//type type  
string password=GetFolderPassword(d);// Gets folder password information  
int type=GetFolderLimitType(d); 
if((password!="no")&&(type!=1)) 
dr[2]="<a 
href=PasswordValidate.aspx?path="+d+"> seal </a>";// operation action 
else 
dr[2]="<a href=Default.aspx?path="+d+"> Enter the </a>";// operation action 
dr[3]=GetFolderOwner(d);// Take the folder creator name  
} 
else 
Response.Write("<script>alert(' No object to bind to !')</script>"); 
dt.Rows.Add(dr);// Add the line  
return dt;// Return data table  
} 
} 

This method takes the contents of the specified folder and processes them separately. If it is a file, the "download" link is displayed to the Url address of the file on the server. The folder needs to be treated differently depending on whether it is protected or not and the type of protection: if "entry is restricted", the "seal" will be displayed to import into the password verification page; if "entry is not restricted", the query string will be modified, and the "entry" link will be displayed to the initial display page.

2. Create a new folder and set access rights:

After entering a new folder name, you can create a new folder in the current path. Asp.net can create a new folder remotely and operate it on the local machine. It is very simple.

Directory.CreateDirectory(string directory)

Where the string directory represents the full path to the new folder, this method by default opens all users to full read and write access to the new folder. Particular, the network hard disk sharing root folder (application example for "NetHard" folder, you can change it) to create you can manually, conversely, you need to manually write access to open this folder, under IIS, you can under the "internet information services", the implementation for the folder "access wizard" to set access permissions.

Security should be considered when sharing resources. Here, you can set the user's access to the folder, and by setting the password, you can specify whether the user is allowed to enter the folder, whether the folder is allowed to be deleted, or both. In this way, you can protect your own file, exclusive access to them or limit in a certain range (members of this range should know your password), in this way, product research and development department can share a folder, the financial department also Shared by the same way the other one folder, do not belong to the department of personnel will be limited access. Of course, you can also apply this restriction to a single file (there don't seem to be many such applications that aren't implemented).

In addition, to manage these folders, you need to keep information about their Settings: folder name, location, restricted operation type, password, and creator. The program writes this information to the 1Xml file (folder.xml file), and then manages the various operations of the folder by reading and writing the xml file. The following method achieves the storage of setting information when creating a new folder:
 
public void CreateXmlOrAddFrag(string xmlFullPath,string 
folderFullPath,string owner,string password,int type){ 
XmlDocument xmlDoc=new XmlDocument(); 
string xml=""; 
string xmlNode="<character>"; 
xmlNode+="<fullPath>"; 
xmlNode+=folderFullPath; 
xmlNode+="</fullPath>"; 
xmlNode+="<owner>"; 
xmlNode+=owner; 
xmlNode+="</owner>"; 
xmlNode+="<password>"; 
xmlNode+=password; 
xmlNode+="</password>"; 
xmlNode+="<type>"; 
xmlNode+=type; 
xmlNode+="</type>"; 
xmlNode+="</character>"; 
if(!File.Exists(xmlFullPath)){// Create if it doesn't exist xml Storing files  
xml="<?xml version='1.0' encoding='gb2312'?>"; 
xml+="<folder>"; 
xml+=xmlNode; 
xml+="</folder>"; 
xmlDoc.LoadXml(xml); 
xmlDoc.Save(xmlFullPath);// Storing files  
} 
else{// There are xml Storing a file adds a new document fragment  
xmlDoc.Load(xmlFullPath); 
XmlDocumentFragment 
docFrag=xmlDoc.CreateDocumentFragment();// Document slice element nodes  
docFrag.InnerXml=xmlNode; 
XmlNode currNode=xmlDoc.DocumentElement;// Gets the document root lake  
currNode.InsertBefore(docFrag,currNode.FirstChild);// Insert document segment  
xmlDoc.Save(xmlFullPath);// Storage change  
} 
} 

Where, parameter fileFullPath is the xml file path to store folder information, folderFullPath is the path to this new folder, owner is the name of the creator, password is the password, type is the type of restricted operation ("0" means restricted entry, "1" means restricted deletion). At the beginning of the program, the folder.xml file storing the folder information may not exist yet, so the program first determines whether the file exists, then creates the file dynamically. After that, when creating a new folder, only the document fragments need to be added. Here, read and write to the Xml file is done through DOM(document object model). Since the Xml file is not too large here, this method will not require too much memory resources and will be very fast!

3. Operation of files and folders:

The operation of the file includes "download" and "delete". When the contents in the specified folder are displayed in the above list, there are such statements for the file:

dr[2]=" < a href="+url+" target=_blank"+" > download < /a > "; // establish the join address

Where url is the server path for the specified file. Use this link to locate the file to its location on the server. Depending on the file format, the client can download the file to its home machine or open it directly on the server.

Deleting files is simple.Net's IO namespace's File class library has methods like this:

File. Delete (string filename); Where, filename is the full path of the file

You need to note that the deletion of the file will not require validation. To prevent the file from being deleted, you need to follow the following steps. (of course, in order to avoid file deletion procedures to provide validation, folder deletion is also 1!)

The operation of the folder is divided into: enter or delete. If the folder is set to "enter restricted" or "delete restricted" at the time of creation, the user will be required to verify the password when performing such an operation. Only through verification can the corresponding operation be completed. This way protects your files from being illegally accessed or deleted.

Relatively file delete folder of remove programs is a little more complicated, the Net Directory class library provides methods Delete () can only delete empty folder, this needs us to empty folder content first, then to complete this operation, 1 due to the folder may also contains a folder again, we achieve this through recursive call 1 operation:
 
// Delete folder method  
public void DeleteFolder(string dir) 
{ 
foreach(string d in Directory.GetFileSystemEntries(dir)) 
{ 
if(File.Exists(d)) 
File.Delete(d);// Delete the files directly  
else 
DeleteFolder(d);// Recursively delete subfolders  
} 
Directory.Delete(dir);// Delete the empty folder  
} 

Where, parameter dir is the full path to the folder to be deleted. The program USES a looping statement to find the contents of the folder. If it is a file, it will be deleted directly. If it is a folder, it will recursively call the method itself to delete the subfolder.

4. File upload:

To realize the function of the network hard disk, there must be available for operating files, these files from the client to upload (of course, can also provide these files on the server, but this is not the focus of the network hard disk), we used to realize the file upload, is usually used in Asp 1 file upload component, such as Microsoft PostingAcceptor components, in addition, a third party to provide 1 pay components (but seriously, these components are not to use). At that time, it was quite tedious to develop such a file upload component by yourself. Now, the class libraries HttpPostedFile and HttpFilesCollection provided by.Net can easily access files uploaded to the server, while giving developers control over the file upload process. The HttpPostedFile class encapsulates file objects that have been uploaded to the server, and its methods and properties provide access to the contents and properties of each file. The HttpFileCollection class provides a container for multiple HttpPostedFile objects to hold the data structures uploaded to the server, so that you can take advantage of the collection of files that have been transferred, which can be accessed from the HttpRequest object through its Files property, and which can be accessed once the server has received the entire content of the request. These built-in components make file uploading in Asp.net quite easy, even if it only takes a few lines of code! There are a lot of articles about file upload in Asp.net. There is no special explanation here. You can refer to those articles if you need to.

1) the encoding type Enctype of the client form is set to multipart/ form-data MIME, and the form is submitted using post method of Http, as follows:

< form id="form1" method="post" enctype="multipart/form-data/form-data" runat="server" >

2) HttpInputFile control runs on the server side, runat=server, type=file, as follows:

< INPUT id="fileUp" type="file" size="6" runat="server" >

3) to upload multiple files, you can lay out multiple HttpInputFile controls and use HttpRequest.Files to get the files.

5. Processing of environment configuration file Web.config:

In Chinese, you may need to change the globalization Settings encoding from the default "Utf-8" to "gb2312", as follows:

 
<globalization 
requestEncoding="gb2312" 
responseEncoding="gb2312" 
/> 


At the same time, the Web.config default file size limit may not be enough for you, and you may need to scale the limit up a bit. This is done by modifying the value of the parameter maxRequestLength, as shown below:
 
<!--  Sets the maximum acceptable number of bytes  --> 
<httpRuntime maxRequestLength="500000"> 
</httpRuntime> 


6. Track the "current path" value at any time:

Another key point in the application is about the "current path", because you always need to jump between different folders: click "enter" to get to the next folder, click "previous folder" to return to the previous folder; The contents of all these folders are displayed on the same page (Default.aspx), which is set to always display the contents under the current folder. When you send a query string to call it, you need to include the full representation of the current path in the query string. This is done in the program by setting a static variable: public static string currFullPath=""; When the current path changes, the path value is always passed to the static scalar:

currFullPath = Request [" path "]; This allows the static variable to always store the current path value, and to call the display page as the query string: Response.Redirect (" Default.aspx? path="+currFullPath) will always display the contents of the current folder!

Reference:

Asp. net programmer reference manual, XML advanced programming, C # advanced programming, Microsoft MSDN

Operating environment:

The program is available in: Windows XP Chinese version. Net Frameworks official version, Visual Studio.Net Chinese official version debugging passed, in the LAN (headquarters + multiple remote subsidiaries mode) stable operation

Related articles: