C # website export Function

Source: Internet
Author: User

This website export function is triggered by the front-end JavaScript to enter the ashx function, so that a folder (including its sub-folders and files) on the server can be copied to another location on the server, of course, the folder itself is a website.

Therefore, the two most important functions of exporting a website, apart from JavaScript triggering, are the operations for copying folders to C # ashx files.

The following sectionCodeThe copy. ashx function file is called through the request function of JavaScript, and two parameters of the sub-path of the folder to be copied and the sub-path to the location are passed to the background function.

The getweblist function is a function in the background, which can be left alone.

The getback function needs to be written. You can use this function to obtain the result.

Of course, the webside_load function also needs to be triggered by onclick, and not all are listed here.

 

 

 1   //  Webside_load  2 Function webside_load (SID, iwebtemplateid ){ // Alert (0 );  3       //  Alert (SID); alert (iwebtemplateid );  4       //  Directory assigned: the folder and file under the template ID  5 Startdir = "  Http://www.cnblogs.com/uploadfile/webTemplate/  " + Iwebtemplateid;  6       //  Target Directory: Under the file ID 7 Senddir = "  Http://www.cnblogs.com/uploadfile/showweb/  " + Sid + "  /  "  ;  8       //  Alert (sourcedir); alert (targetdir );  9   10       VaR Variable = [ " Startdir  " , "  Senddir  "  ];  11       VaR Value = [startdir, senddir]; //  Alert (value );  12 Request ( "  Getweblist  " , Variable, value, getback, weburl + "Http://www.cnblogs.com/ Copy. ashx  "  , Svrnamespace );  13   14   }  15   Function getback (){  16       VaR XMLHTTP = XMLHttpRequest;  17       VaR Result = XMLHTTP. responsetext;  18   Alert (result ); 19   }  20      

Through the Javascript above, you can obtain data from the background functions.

To obtain the data of the background function, we need to traverse the function file with copy.

This function was written after I learned about the writing method. Du Niang is still very powerful and has seen many good methods, but I feel that the following method is more desirable.

 1 <% @ Webhandler Language = "  C #  " Class = "  Copy  " %> 2   3   Using System;  4   Using  System. Web;  5   Using  System. IO;  6   7   Public   Class  Copy: ihttphandler  8   {  9       //  Recursively traverse all files in folders and subfiles. 10       Public   Void  Processrequest (httpcontext context)  11   {  12 Httprequest request = Context. request;  13 Httpresponse response = Context. response;  14 Httpserverutility Server = Context. server;  15           // Specify the output header and encoding  16 Context. response. contenttype = "  Text/html  "  ;  17 Response. charset = "  UTF-8  "  ;  18   19 Httpfilecollection FS = Httpcontext. Current. Request. files;  20          21           String Startdir = request. Form [ "  Startdir  "  ];  22           String Senddir = request. Form [ "  Senddir  "  ];  23 Startdir = System. Web. httpcontext. Current. server. mappath (startdir );  24 Senddir = System. Web. httpcontext. Current. server. mappath (senddir );  25           //  Test  26           //  String startdir = system. Web. httpcontext. Current. server. mappath ("../uploadfile/webtemplate/2 ");  27           //  String senddir = system. Web. httpcontext. Current. server. mappath ("../uploadfile/showweb/2012082700000001 /");  28   Mydirectory_copy (startdir, senddir ); 29           30 Response. Write ( "  Exported successfully!  "  );  31   }  32       33       Static   Void Mydirectory_copy ( String Startdir, String  Senddir)  34  {  35           //  Determine whether both directories exist  36           If (! Directory. exists (startdir ))  37               Return  ;  38           If (! Directory. exists (senddir ))  39               Return  ; 40   41           //  Get folder name  42           String Startefoldername = startdir. Replace (directory. getparent (startdir). tostring (), "" ). Replace (path. directoryseparatorchar. tostring (), ""  );  43           //  Determines whether the folder has been assigned a value.  44           If (Startdir = senddir +Startefoldername)  45               Return  ;  46   47           //  Path to be copied  48           String Endpath = senddir + path. directoryseparatorchar. tostring () + Startefoldername;  49           If  (Directory. exists (endpath ))  50  {  51 Directory. Delete (endpath, True  );  52   }  53   54   Directory. createdirectory (endpath );  55   56           //  Copy a file  57           String [] Files =Directory. getfiles (startdir );  58           For ( Int I = 0 ; I <files. length; I ++ )  59   {  60 File. Copy (files [I], endpath + path. directoryseparatorchar. tostring () + Path. getfilename (files [I]);  61   }  62           // Copy directory  63           String [] Dires = Directory. getdirectories (startdir );  64           For ( Int J = 0 ; J <dires. length; j ++ )  65   {  66   Mydirectory_copy (dires [J], endpath );  67   } 68   }  69   70     71   72       Public   Bool  Isreusable  73   {  74           Get  75   {  76               Return   False ;  77   }  78   }  79 }

 

Obtain the startdir source file directory and startdir target directory from the foreground to obtain their absolute path.

Then execute the directorycopy function,

Obtain the folder name of the source file, and add the absolute path of the target file to the new directory of the target file.

Execute the replication process through recursive loop judgment.

 

This method is similar to the C # traversal folder mentioned above, but it facilitates internal replication, and is not exactly the same as traversing in the system. This is relatively simple, but at the beginning I used the baidu search method of DU Niang.

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.