Network hard drive development skills-ASP. NET + XML

Source: Internet
Author: User

FTP, Email, and "Network neighbors" are commonly used for file data transmission to a certain extent, but they are mainly for "point-to-point" transmission, it is impossible to meet the application requirements of "One space, Resource Interaction". This "Point-to-multiple" sharing mode requires another transmission path. Network hard disk is a good solution. Common transmission methods and the difficulties we face in a specific environment we often have the need to exchange public data files over the network for resource sharing, while protecting private data from unauthorized access, and easy to use and intuitive operation.

 

Ewedrive provides 1 GB of free storage space, but it does not limit the size of a single file.

Our common file transmission methods include FTP, Email, and network neighbors. Among them, "Ftp" is the most powerful, but it is a little complex to use, a lot of settings are enough to let many people stop, especially when the number of users is unpredictable, user settings for special needs will be more complicated; "Email" is familiar to everyone, but its transmission requires you not only to connect to the Internet, but also its security is also a problem, within an enterprise, it is not a good way to exchange documents such as finance and labor resources. "Network neighbors" are another method for transferring documents, by specifying shared Web folders on the local machine and placing resource files, users within a certain range can access these files. However, this method is widely used, generally, users in the same DNS segment can access the domain name smoothly. Other users, especially those on the Internet, are difficult to use. In addition, similar to Email, the domain name is not intuitive, most of the time, you have to expand the search layer by layer on many list computers to obtain the resources you want! From this point of view, although the above three methods have achieved the communication of file data to a certain extent, however, they are mainly oriented to "point-to-point" transmission (you can only passively wait for the other party to place data, rather than actively "Visually" requests), and cannot implement "one space, resource Exchange "application requirements, this" Point-to-multiple "based sharing method needs to seek another transmission channel, network hard disk is a good solution.

 

Onedump

The OneDump bucket is not limited in size, but a single file is only 5 MB in size and 10 files are transferred at the same time, which can be used to store documents and other things.

The advantage of a network hard disk, also known as a shared space, is a hard disk space on the server. Here, if you have sufficient permissions, you can operate on it at will, just like using your local computer. You must know that all of this is transmitted over Http and displayed in front of all users in the form of Web. You can use a browser to access it. In this way, "a space, the sharing mode of resource mutual view is realized! In addition, the network hard disk interface runs on the client. It allows users to submit data to the server and manage the information within the specified range. This processing mode is widely used in actual development, especially in the construction of an interactive network document management system: Online examination system, file transmission system, news and publishing system, and the company bidding system have a large number of applications! The following uses asp.net (implemented in Visual C #) and Xml (Extensible Markup Language) to develop such a network hard drive system. Detailed descriptions of program development details and Key Technologies and difficulties used in code are provided. You can develop your own network hard drive by referring to these instructions.

Network hard disk development: functions of network Hard Disks

Our network hard drive provides the following features:

1. View folder content

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

3. jump between folders: Go to the next level and return to the previous level

4. upload a file to a specified folder

5. download the file to your local computer or view the file content online.

6. delete files or folders

Network hard disk development details and key technical points

1. View folder content

At the beginning, the program will enter the specified root folder (such as \ NetHard). The content in this folder will be displayed in the list through the data binding control (DataGrid), including the following: category (difference folder or file), name, permission (whether to allow access to the folder? Can an object be downloaded ?) , Delete (Are you allowed to delete files or folders ?) And the creator of the folder. Here, because the content in the folder changes dynamically (you Don't know when a user transfers files, creates folders, or deletes them ), we dynamically create a data table able to read the content in the specified folder and bind it to the display control DataGrid as a data source. In this way, data display is always timely. The function for creating a dynamic table is as follows: public DataTable Bind (string fullFolderPath) // create a data table to read the folder content

 
 
  1. {
  2. // The following describes how to dynamically create a data table: 
  3. DataTable dt =NewDataTable ();
  4. DataRow dr;
  5. // Create a data table structure 
  6. Dt. Columns. Add (NewDataColumn ("Type", System. Type. GetType ("System. String")));// Type 
  7. Dt. Columns. Add (NewDataColumn ("Name", System. Type. GetType ("System. String")));// Name 
  8. Dt. Columns. Add (NewDataColumn ("Action", System. Type. GetType ("System. String")));// Operation 
  9. Dt. Columns. Add (NewDataColumn ("Owner", System. Type. GetType ("System. String")));// Creator 
  10. // Fill in data for each row 
  11. Foreach(StringDInDirectory. GetFileSystemEntries (fullFolderPath )){
  12. Dr = dt. NewRow ();// Create a new row 
  13. String[] Parts = d. Split (New Char[] {'\\'});
  14. StringTxt = parts [parts. Length-1];// Obtain the last part of the string, which may be a file name or folder 
  15. Dr [1] = txt;// Name 
  16. If(File. Exists (d ))// If it is a file 
  17. {
  18. Dr [0] ="File";// Type 
  19. IntPos = currFullPath. IndexOf ("NetHard");
  20. StringRelaUrl = currFullPath. Substring (pos );
  21. String
  22. Url ="Http: // 10.80.50.1/SharedSpace /"+ RelaUrl +"/"+ Txt ;//
  23. 10.80.50.1 is the author's server address. You should modify it to your server address.
  24. Dr [2] ="Download";
  25. }
  26. Else If(Directory. Exists (d )){// For folders 
  27. Dr [0] ="Folder";// Type 
  28. StringPassword = GetFolderPassword (d );// Obtain the folder password information 
  29. IntType = GetFolderLimitType (d );
  30. If(Password! ="No") & (Type! = 1 ))
  31. Dr [2] ="Href = PasswordValidate. aspx? Path ="+ D +"> Seal";// Action 
  32. Else
  33. Dr [2] ="Enter";// Action 
  34. Dr [3] = GetFolderOwner (d );// Obtain the name of the folder creator. 
  35. }
  36. Else
  37. Response. Write ("");
  38. Dt. Rows. Add (dr );// Add rows 
  39. ReturnDt;// Return the data table 
  40. }

2. Create folders and set Access Permissions

Enter a new folder name in the current path to create a new folder. net IO namespace class library Directory method to achieve this operation: Directory. createDirectory (string directory), string directory indicates the complete path of the new folder. By default, this method gives all users full read/write permissions for the new folder. You can manually create a root folder for network hard drive sharing and set the write permission. You can set the access permission by executing the "permission wizard" of "internet information service, you can set a password to specify whether to allow users to access the folder and whether to allow users to delete the folder. In addition, to manage these folders, You need to retain the relevant settings, such as the folder name, location, restricted operation type, password, and creator. Write the information to the XML file, and then perform operations on the folder by reading and writing the XML file.

 
 
  1. PublicVoidCreateXmlOrAddFrag (StringXmlFullPath,StringFolderFullPath,StringOwner,StringPassword,IntType ){
  2. XmlDocument xmlDoc =NewXmlDocument ();
  3.  StringXml ="";
  4. String xmlNode ="<Character>";
  5. XmlNode + ="<Full Path>";
  6. XmlNode + = folderFullPath;
  7. XmlNode + ="</Full Path>";
  8. XmlNode + ="<Owner>";
  9. XmlNode + = owner;
  10. XmlNode + ="</Owner>";
  11. XmlNode + ="<Password>";
  12. XmlNode + = password;
  13. XmlNode + ="</Password>";
  14. XmlNode + ="<Type>";
  15. XmlNode + = type;
  16. XmlNode + ="</Type>";
  17. XmlNode + ="</Character>";
  18.  If(! File. Exists (xmlFullPath )){// Create an xml storage file if it does not exist 
  19. Xml ="<? Xml version = '1. 0' encoding = 'gb2312 '?> ";
  20. Xml + ="<Folder>";
  21. Xml + = xmlNode;
  22. Xml + ="</Folder>";
  23. XmlDoc. LoadXml (xml );
  24. XmlDoc. Save (xmlFullPath );// Store files 
  25. }
  26.  Else{// Add a new file segment if an xml storage file exists. 
  27. XmlDoc. Load (xmlFullPath );
  28. XmlDocumentFragment docFrag = xmlDoc. CreateDocumentFragment ();// File Element Node 
  29. DocFrag. InnerXml = xmlNode;
  30. XmlNode currNode = xmlDoc. DocumentElement;// Get the document root node 
  31. CurrNode. InsertBefore (docFrag, currNode. FirstChild );// Insert a document clip 
  32. XmlDoc. Save (xmlFullPath );// Store changed content 
  33. }
  34. }

The fileFullPath parameter is the XML file path for storing Folder Information, folderFullPath is the path of this new folder, owner is the creator name, and password is the password, type indicates the type of the restricted operation. "0" indicates that the access is restricted, and "1" indicates that the deletion is restricted ). The program first checks whether the file storing the folder information exists. If the file does not exist, it dynamically creates the file. When creating a folder, you only need to add a document segment. Here, the reading and writing of XML files is implemented through the DOM of the Document Object Model. Because the XML file is not large here, the requirements for memory resources are not too high and the speed will be very fast.

3. Operations on files and folders

Operations on files include "Download" and "delete". Use the following statement to download files:

 
 
  1. Dr [2] ="<A href ="+ Url +"Target = _ blank"+"> Download </a>";// Create a connection address 

The url is the server path corresponding to the specified file. You can use this link to locate the file on the server. The client can download the file to the local machine or open it on the server.

To Delete a File, use the method in the File class library of the. net io namespace: File. Delete (string filename); where filename is the complete path of the File.

If the folder is set to "restricted access" or "restricted deletion" during creation, the user will be asked for password verification when performing such an operation, the corresponding operation can be completed only after the verification is passed .. The Delete () method provided by the Directory class library of. NET can only Delete empty folders. Therefore, you need to clear the contents of the folder before you can complete this operation. Because the folder may contain folders, this operation is implemented by recursive calling:

 
 
  1. PublicVoidDelete Folder (StringDir)
  2. {
  3. For each (StringDInDirectory. GetFileSystemEntries (dir ))
  4. {
  5. If (File. Exists (d ))
  6. File. Delete (d );// Delete the objects directly 
  7. Else
  8. Delete Folder (d );// Recursively Delete subfolders 
  9. }
  10. Directory. Delete (dir );// Delete an empty folder 
  11. }

The dir parameter is the complete path of the folder to be deleted. The program uses a loop statement to find the content in the folder. if the content is a file, it deletes the content directly. If the content is a folder, it recursively calls the method itself to delete the subfolders. After the folder is cleared, the deletion operation is completed.

4. File Upload

The class libraries HttpPostedFile and HttpFilesCollection provided by. NET can access the files uploaded to the server and control the File Upload process. The HttpPostedFile class encapsulates the file objects uploaded to the server. Its method and attributes provide access to the content and attributes of each file. The HttpFilesCollection class provides a container for multiple HttpPostedFile objects, the class that stores the data structure uploaded to the server. It is accessed from the HttpRequest object by using the Files attribute of the transferred file set.

1) Enctype, the encoding type of the client form, is set to the MIME format of multipart/form-data. submit the form using the HTTP post method:

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

2) The HttpInputFile control runs on the server. The setting method is as follows:

 
 
  1. <INPUT id="file Up" type="file" size="6" runat="server">  

3) If you want to upload multiple Files, you can set multiple HttpInputFile controls and use HttpRequest. Files to obtain these Files respectively.

5. Track the "current path" Value

Because you need to jump between different folders, and the content of all folders is routed through the same page Default. ASPx to display. When sending a query string to call it, you can set a complete representation of the static variable implementation path: public static string currFullPath = "". When the current path changes, the path value is sent to this static variable, which is always stored in the current path value and used as the query string. The call display page is displayed: currFullPath = Request ["path"]. Therefore, use Response. Redirect ("Default. aspx? Path = "+ currFullPath) statement can always display the content in the current folder.

6. Environment configuration file Web. config Processing

In Chinese, you need to change the global settings encoding from the default "Utf-8" to "gb2312 ":

 
 
  1.    <Globalization   
  2. Request Encoding="gb2312"   
  3. Response Encoding="gb2312"   
  4. />   

At the same time, considering that the file size set by default by Web. config may not meet the actual needs, you can modify the value of maxRequestLength to enlarge its scale as needed:

 
 
  1. <HttpRuntime maxRequestLength="500000">   
  2. </httpRuntime>  

Conclusion

Asp.net and XML are currently hot topics in network development and application, and are widely used in data transmission, Information Publishing, electronic payment, network security, and many other fields. The technology is used together for network hard disk development to share system resources, which not only meets users' basic needs and is easy to operate, but also effectively protects users' private data from unauthorized access, it is secure and easy to maintain. Therefore, this processing mode can be widely used in practical applications such as the document transmission system, online examination system, and news publishing system.

  1. Application of session storage mode in ASP. NET
  2. Set of methods for uploading and downloading files in ASP. NET
  3. Introduction to cookie read/write methods in ASP. NET
  4. Javascript operations in ASP. NET
  5. Introduction and implementation of Single Sign-On in ASP. NET2.0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.