Asp.net+xml Development Network Hard disk

Source: Internet
Author: User
Tags config empty file size file upload ftp http post include object model

asp.net|xml| Network | Hard disk File transfer commonly used three ways FTP, email and "Network Neighborhood" to a certain extent to achieve the exchange of file data, but they are mainly oriented to "point-to-point" transmission, can not achieve "a space, resources are mixed" application needs, this based on "point to many" The sharing mode needs to seek another transmission way, the network hard disk is a very good solution.


Common transmission modes and the difficulties they face in a given environment


We often have applications like this: exchanging public data files over a network for resource sharing while protecting private data from unauthorized access and operating in simple, intuitive ways. Our commonly used file transfer FTP, email mail, Network Neighborhood can achieve the transfer of files. Among them, the "FTP" function is the most powerful, but it is slightly more complicated to use, a large set of settings enough to make many people Shing, especially when the number of users is unpredictable, the user's settings for special needs will be more cumbersome; "Email" is familiar to you, but it's not only the transmission need you to connect to the Internet, And its security is a problem, in the enterprise, financial, labor and other documents to communicate in this way is not a good way; "Network Neighborhood" is another way to transfer files, by specifying a shared Web folder on this computer and placing a resource file, a range of users can access the files, However, the scope of this approach is very limited, usually in the same DNS segment address users can access the smooth, other especially the Internet users, it is difficult to use, in addition to email similar to its use is not intuitive, Many times you have to start a search on many list computers to get the resources you want!

In this view, the above three ways, although to a certain extent, to achieve the exchange of document data, but they are mainly oriented to the "point-to-point" transmission (you can only passively wait for each other to place data and can not actively "visual" to obtain), can not achieve "a space, resources are mixed" application needs, this based on "point to many" The sharing way needs to seek another transmission way, the network hard disk is a very good solution way.


Advantages of network hard disk


Network hard disk (also known as shared space), is a disk space on the server, where, if you have sufficient permissions, you can operate it at will, as if using your local computer. To be aware, all this is transmitted in HTTP, in the form of the web in front of all users, through the familiar way of browser access, so that "a piece of space, resources are mixed" sharing mode to achieve!

In addition, the network hard disk interface runs on the client, which allows the user to submit data to the server and then manage the information in a specified range, which is heavily used in actual development, especially in building interactive network document management systems: Online exam systems, file transfer systems, The press release system as well as the company bids the system and so on has the massive application!

The following uses the ASP.net (Visual C # language implementation) and XML (Extensible Markup Language) to develop such a network hard disk system. The details of the development of the program and the key techniques and difficulties used in the code are explained in detail, and you can make your own network hard drive by referring to these instructions.


The function of network hard disk implementation


Our network hard disk implements the following features:

1. View Folder Contents

2, create a new folder can also set access rights

3. Jump between folders: Go to the next level, return to the previous level

4, upload files to the designated folder

5, download files to the local or online view the contents of the file

6. Delete files or folders


Development details and technical key points


1. View Folder Contents:

At the beginning of the program, the specified root folder (such as \nethard) is entered, and the contents of the folder are displayed in the data-bound control (DataGrid) list, which includes the following: categories (different folders or files), names, permissions (is it allowed to enter the folder?). Can i download a file? , delete (allow files or folders to be deleted?) ) and the creator of the folder. Here, because the contents of the folder are dynamically changing (you don't know when a user has sent files or created a new folder or deleted them), we are dynamically creating a DataTable to read the contents of the specified folder and then bind the DataGrid as the data source to the display control, so that The display of data is always timely. The function to create a dynamic table is as follows:

Public DataTable Bind (string fullfolderpath)//Create datasheet Read folder contents
{
Here's how to create a data table dynamically
DataTable dt=new DataTable ();
DataRow Dr;
First create 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"));//Name
Dt. Columns.Add (New DataColumn ("Action", System.Type.GetType ("System.String"));/operation
Dt. Columns.Add (New DataColumn ("owner", System.Type.GetType ("System.String"))//Create person
Fill in data for each row
foreach (String d in Directory.getfilesystementries (Fullfolderpath)) {
Dr=dt. NewRow ();//Jianning
String[] Parts=d.split (new char[]{' \ '});
String Txt=parts[parts. length-1];//take the last part of the string, it will probably be the file name or folder
dr[1]=txt;//Name
if (file.exists (d))//If it is a file
{
dr[0]= "file";//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 is a folder
dr[0]= "folder";//type type
String Password=getfolderpassword (d);//Get 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 </a>";/Operation Action
Dr[3]=getfolderowner (d);//Fetch folder creator name
}
Else
Response.Write (' <script>alert (' no object bindable! ') </script> ");
Dt. Rows.Add (DR)//Add row
Return dt;//data table
}
}



This method takes out the contents of the specified folder and handles it separately: if it is a file, the "Download" link is displayed to the URL of the file on the server, the folder needs to be protected or not, and the protection type is further differentiated: if "enter restricted", the "seal" is imported to the Password verification page, "Access is unrestricted" The query string is modified, and the "enter" link to the initial display page is displayed.

2, create a new folder and set access rights:

After entering the new folder name, you can create a new folder under the current path, asp.net the remote creation of a new folder is exactly the same as the local machine operation, the. Net IO namespace provides a dedicated class library directory that can be implemented by calling its methods, as follows:

Directory.CreateDirectory (String directory)



Where the string directory represents the full path to a new folder, which by default opens full read and write access to the new folder to all users. In particular, share the root folder on the network hard disk (the program example is "Nethard" folder, you can change it) the creation you can also manually, correspondingly, you need to manually set up to open this folder Write permission, under IIS, you can under "Internet Information Services", Perform the Permissions wizard on this folder to set access permissions.

Security should be considered when sharing resources. Here, you can set the user access to the folder, by setting a password, you can specify whether to allow users to enter the folder, whether to allow the deletion of the folder, or both. In this way, you can protect your own files, exclusive access or limited to a specific scope (the members of this scope should know your password), so that the product Development department can share a folder, the financial department in the same way to share another file folder, people who do not belong to this department will restrict access. Of course, such restrictions you can apply to a single file (such applications do not seem to be much, the program is not implemented).

In addition, to manage these folders, you need to keep their settings information: folder name, location, restricted action type, password, and creator. The program writes this information to an XML file (this is a folder.xml file), and then the various administrative operations on the folder are read and written to the XML file, and the following methods implement the storage that sets the information when the new folder is created:

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)) {//No existing to create an XML store file
Xml= "<?xml version= ' 1.0 ' encoding= ' gb2312 '?> '";
xml+= "<folder>";
Xml+=xmlnode;
xml+= "</folder>";
Xmldoc.loadxml (XML);
Xmldoc.save (Xmlfullpath)//storage file
}
else{//existing XML store file add new document fragment
Xmldoc.load (Xmlfullpath);
XmlDocumentFragment
Docfrag=xmldoc.createdocumentfragment ();//Document SLICE element node
Docfrag.innerxml=xmlnode;
XmlNode currnode=xmldoc.documentelement;//Get Document Root section
Currnode.insertbefore (docfrag,currnode.firstchild)//Insert Document Segment
Xmldoc.save (Xmlfullpath);//Storage change
}
}



Where the parameter filefullpath is the path to the XML file where the folder information is stored, Folderfullpath is the new folder path, owner is the person who created the name, password is the password, and type denotes the types of the restriction action ("0" means enter restricted, "1" Indicates deletion restriction). At the beginning of the program, the Folder.xml file that stores the folder information may not yet exist, so the program first determines whether the file exists, does not exist, creates the file dynamically, and when you create a new folder, you only need to add the document fragment. Here, the XML file is read and written through the DOM (Document Object model), because the XML file here is not too large, this way will not be too high for memory resources, speed will be fast!

3, the operation of files and folders:

Actions on a file include "Download" and "delete", and when the list shows the contents of the specified folder, there are statements for the file:

dr[2]= "<a href=" +url+ "Target=_blank" + "> Download </a>";//Establish a join address



Where the URL is the server path for the specified file. Navigate the file to the corresponding location on the server with such a link. Depending on the format of the file, the client can download the file to the computer, or it can be opened on the server.

Deleting the file is simple. NET's IO namespace, there is a method for the File class library:

File.delete (string filename), where filename is the full path of the file

You need to be aware that the deletion of the file will not need to be validated, to prevent the file from being deleted, you need to follow the following way. (Of course, in the program to avoid the deletion of files to provide confirmation verification, folder deletion is the same!) )

The operation of the folder is divided into: Enter or delete. If a folder is set to "enter restricted" or "delete restricted" when it is created, the user will be required to authenticate the password when performing such an operation, which protects your files from being illegally accessed or deleted.

The deletion of the folder is slightly more complicated than the deletion of the file. NET Directory Class Library provides methods delete () can only delete empty folders, which requires us to first empty the contents of the folder before we can complete this operation, because the folder may also contain folders, we do this by recursive call to achieve this action:

Ways to delete a folder
public void DeleteFolder (string dir)
{
foreach (String D in Directory.getfilesystementries (dir))
{
if (file.exists (d))
File.delete (d);//delete files directly from them
Else
DeleteFolder (d);//recursively Delete subfolders
}
Directory.delete (dir);//Delete empty folder
}



Where the parameter dir is the full path to the folder you want to delete. The program uses a looping statement to find the contents of the folder, the file is deleted directly, if it is a folder, then recursively call the method itself to delete the subfolder, the folder empty and then complete the delete operation.

4, File Upload:

To achieve the function of the network hard disk, there must be files available for operation, which are uploaded from the client (of course, the server side can provide these files, but this is not the focus of the network hard disk), in the past we implemented the file upload, in the ASP is usually using the first file upload components, such as Microsoft Postingacceptor components, plus some of the paid components provided by third parties (but really, these components are not working). At that time wanted to develop such a file upload component, quite cumbersome; NET provides the class libraries Httppostedfile and httpfilescollection can easily access files uploaded to the server, while allowing developers to control the file upload process. The Httppostedfile class encapsulates a file object that has been uploaded to the server, whose methods and properties provide access to the contents and properties of each file, and the Httpfilecollection class provides a container for multiple Httppostedfile objects. Used as a class to save data structures to the server, so you can take advantage of the transferred collection of files that can be accessed from the HttpRequest object through its Files property, which can be accessed once the server receives the entire content of the request. These built-in components make it easy to upload files in asp.net, even if only a few lines of code are required! About asp.net in the implementation of file upload a lot of articles, here do not make a special exposition, you can refer to those articles, here, only tips you need to pay attention to the following points:

1), the encoding type of the client form enctype set to multipart/form-data MIME format, submit the form using the HTTP POST method, as follows:

<form id= "Form1" method= "Post" enctype= "Multipart/form-data/form-data"
runat= "Server" >



2), Httpinputfile control run on the server side, set the 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 then use Httprequest.files to obtain these files.

5, the Environment configuration file Web.config processing:

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

<globalization
Requestencoding= "gb2312"
Responseencoding= "gb2312"
/>



Also, the file size limit web.config by default may not meet your needs, and you may need to magnify the limits. This is done by modifying the value of the parameter maxRequestLength, as follows:

<!--set the maximum number of bytes that can be accepted-->
<httpruntime maxrequestlength= "500000" >
</httpRuntime>



6, at any time to track the "Current Path" value:

Another key point in the program is about "current path", because you always need to jump between different folders: Click "Enter" to reach the next level folder, click "Top Folder" to return to the previous level folder, and all of these folders are displayed through the same page (Default.aspx) , this display page is set to always display the contents of the current folder. When you send a query string to invoke it, you need to include a full representation of the current path in the query string, which is implemented by setting a static variable in the program: public static string Currfullpath= ""; This path value is always passed to this static scalar: currfullpath=request["path"; This allows the static variable to always store the current path value, with this static variable calling the display page for the query string: Response.Redirect (" Default.aspx?path= "+currfullpath" can always display the contents of the current folder!

Resources:

"ASP.net Programmer's Reference manual", "Advanced XML Programming", "C # Advanced Programming", Microsoft MSDN

Operating Environment:

Program in: Windows XP Chinese official edition,. Net Frameworks official version, Visual Studio.NET Chinese version of the debugging through, in the LAN (Headquarters + multiple off-site subsidiary mode) on the stable operation


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.

Tags Index: