Last week, the boss asked me to upload folders based on the project's optimization needs. I have never done this before. It's okay to upload a single file and upload multiple files. How can I upload folders? There is no way to get started. Baidu is also rare, and there are not many ideas.Asp.net does not directly select folders. I don't know. If you have any, you can share them.Later, I thought there should be three methods: ① first compress the folder and then upload it to the server, and then decompress it on the server; ② obtain the folder name and directory, and then traverse the files and subfolders under the folder, upload cyclically; ③ use the acitivex control. The boss agreed to the second type.
Then I decided to first get the folder name and the path of the system file where the folder is located through the upload dialog box, but then I was stunned. At first I wanted to use JavaScript to traverse the folder.
1VaRFSO =NewActivexobject ("scripting. FileSystemObject");2VaRF =FSO. getfolder (document. All. fixfolder. value );3VaRFc =NewEnumerator (F. files );
However, if the traversal fails, you will find that you want to create a FSO object.CompositionThe file must have sufficient permissions, which is too troublesome. So I use C # to traverse the folder and write an ashx file, send the browsed data in HTML through action
1 <% @ Webhandler Language = " C # " Class = " Folder " %> 2 3 Using System; 4 Using System. Web; 5 Using System. IO; 6 7 Public Class Folder: 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 Response. contenttype = " Text/html " ; 17 Response. charset = " UTF-8 " ; 18 19 Httpfilecollection FS = Httpcontext. Current. Request. files; 20 String Newfilepath = request. Form [ " Spath " ]; 21 If (FS. Count> 0 ) 22 { 23 // The dirpath of FS [0] corresponding to findfile is the specified directory, and newfilepath absolutely wins svrpath is the target directory, that is, the directory on the server 24 Findfile (FS [ 0 ]. Tostring (), newfilepath ); 25 } 26 Response. Write ( " <SCRIPT> parent. fileuploaddeal () </SCRIPT> " ); 27 } 28 // Recursively traverse all files in folders and subfiles. 29 Public Void Findfile ( String Dirpath, String Svrpath) // The dirpath parameter is the specified directory, and svrpath is the target directory. 30 { 31 // Target directory, that is, the directory on the server 32 String Sfilepath = System. Web. httpcontext. Current. server. mappath (svrpath ); 33 // String sfilepath = system. Web. httpcontext. Current. server. mappath (request. Form ["svrpath"]); 34 // Create a folder 35 If (! Directory. exists (sfilepath )) 36 Directory. createdirectory (sfilepath ); 37 38 // Search for files in the specified directory and subdirectory 39 Directoryinfo dir = New Directoryinfo (dirpath ); 40 Try 41 { 42 Foreach (Directoryinfo d In Dir. getdirectories ())// Find subdirectories 43 { 44 Findfile (DIR + D. tostring () + " \\ " , Svrpath + D. tostring () + " \\ " ); 45 // Findfile (DIR + D. tostring () + "\", svrpath + D. tostring () + "\"); 46 } 47 Foreach (Fileinfo F In Dir. getfiles ()) // Search for files 48 { 49 // F. saveas (server. mappath (svrpath + F. tostring ())); // If you want to save it to another place, make sure to modify it here. 50 F. copyto (system. Web. httpcontext. Current. server. mappath (svrpath + F. tostring ()), True ); 51 Httpcontext. Current. response. Write ( " 4554132 " ); 52 } 53 } 54 Catch (Exception E) 55 { 56 ; 57 } 58 59 } 60 61 Public Bool Isreusable 62 { 63 Get 64 { 65 Return False ; 66 } 67 } 68 }
it was originally thought that the results would be achieved, but a fatal problem was discovered! because the fileupload control itself does not support folder upload, it cannot be assigned a value even through ashx. For more information, we know that, due to security reasons, it is impossible to directly upload a local folder through Code in a browser, activeX control is required.
security permission analysis is indeed not allowed, otherwise, I will write a webpage embedded in this JS Code. Once you open this webpage, JS will start to traverse your hard disk and upload all your files to the server. Only files selected by the input control can be uploaded. Therefore, an upload failure can only be declared if you use this method !! If you have any friends, please feel free to contact me!