C Error Case Analysis of Uploading All Files in Folder After Traversing Folder

  • 2021-08-12 03:18:14
  • OfStack

asp. net is not directly select the folder of the control, I do not know, if you have the words can be a communication. Later, I thought there should be three ways:

(1) Compress the folder and upload it to the server, and then decompress it on the server;

2. Obtain the folder name and directory, then traverse the files and subfolders under the folder and upload them circularly;

③ Is to use AcitiveX control.
That I decisively first through the upload dialog box to get the folder name and folder in the system file path, but then I was appalled, 1 is to use javascript to traverse the folder

1 var fso = new ActiveXObject("Scripting.FileSystemObject");
2 var f = fso.GetFolder(document.all.fixfolder.value);
3 var fc = new Enumerator(f.files);
But find traversal can not, just know to want to create FSO object, operation file, must have enough authority to the file, so too much trouble, so I take C # to traverse the folder, through writing a ashx file, in html through action will browse the data to transfer over

The following is a snippet of all file references under the Upload folder after C # traverses the folder:


<%@ WebHandler Language="C#" Class="folder" %>
 
 using System;
 using System.Web;
 using System.IO;
 
 public class folder : IHttpHandler
 {
   // Recursively traverse all files in folders and subfiles. 
   public void ProcessRequest(HttpContext context)
   {
     HttpRequest Request = context.Request;
     HttpResponse Response = context.Response;
     HttpServerUtility Server = context.Server;
     // Specify the output header and encoding 
     Response.ContentType = "text/html";
     Response.Charset = "utf-8";
 
     HttpFileCollection fs = HttpContext.Current.Request.Files;
      string newFilePath = Request.Form["sPath"];
     if(fs.Count>0)
     {
       //fs[0] Correspondence FindFile Adj. dirPath Is to specify the directory, newFilePath Absolutely win svrPath Is the target directory, which is the directory on the server 
       FindFile(fs[0].ToString(), newFilePath);
     }
     Response.Write("<script>parent.FileUploadDeal()</script>");
   }
   // Recursively traverse all files in folders and subfiles. 
   public void FindFile(string dirPath,string svrPath) // Parameter dirPath For the specified directory ,svrPath Is the target directory 
   {
     // Target directory, that is, the directory on the server 
     string sFilePath = System.Web.HttpContext.Current.Server.MapPath(svrPath);
     //string sFilePath = System.Web.HttpContext.Current.Server.MapPath(Request.Form["svrPath"]);
     // Create a folder 
     if (!Directory.Exists(sFilePath))
       Directory.CreateDirectory(sFilePath);
     
   // Find files in specified directories and subdirectories 
   DirectoryInfo Dir=new DirectoryInfo(dirPath);
   try
   {
     foreach(DirectoryInfo d in Dir.GetDirectories())// Find subdirectories  
     {
       FindFile(Dir+d.ToString()+"\\",svrPath+d.ToString()+"\\");
       //FindFile(Dir+d.ToString()+"\" , svrPath+d.ToString()+"\");
     }
     foreach(FileInfo f in Dir.GetFiles()) // Find a file 
     {
       //f.SaveAs(Server.MapPath(svrPath + f.ToString()));// If you want to save it somewhere else, pay attention to modify it here 
       f.CopyTo(System.Web.HttpContext.Current.Server.MapPath(svrPath + f.ToString()), true);
       HttpContext.Current.Response.Write("4554132");
     }
   }
   catch(Exception e)
   {
     ;
   }
 
   }
 
   public bool IsReusable
   {
     get
     {
       return false;
     }
   }  
 }

I thought this could achieve results, but I found a fatal problem! Because the Fileupload control itself does not support uploading folders, it cannot be assigned a value even through ashx. For more information, I learned that for security reasons, it is not possible to upload local folders directly from the browser through code, but only through the ActiveX control.
From the analysis of security rights, it is really not allowed, otherwise I write a web page, which embeds this js code. If you open this web page, js can start to traverse your hard disk slowly and upload your files to the server. Only files selected by the user through the input control are allowed to be uploaded.
This article is only this site to solve the problem of a train of thought is not a correct method, the purpose is to learn and communicate with everyone to get a better solution.


Related articles: