There is no control to directly select folders, and I don’t know. If you have one, you can communicate with each other. Later I thought there should be three methods:
① First compress the folder and upload it to the server, and then unzip it on the server;
② Obtain the folder name and directory, then traverse the files and subfolders below the folder and upload them in a loop;
③ is to use ActiveX controls.
Then I decisively obtained the folder name and the system file path where the folder is located through the upload dialog box, but then I was shocked. At the beginning, I wanted to use javascript to traverse the folder.
1 var fso = new ActiveXObject("");
2 var f = ();
3 var fc = new Enumerator();
But I found that I couldn't traverse it, so I learned that in order to create an FSO object and operate a file, I must have enough permissions to the file. This is too troublesome. So I used C# to traverse the folder, and wrote an ashx file and transferred the browsed data through action in the html.
The following are all file reference fragments in the folder after C# traversing the folder:
<%@ WebHandler Language="C#" Class="folder" %> using System; using ; using ; public class folder : IHttpHandler { //Recursively traverse all files in folders and subfiles. public void ProcessRequest(HttpContext context) { HttpRequest Request = ; HttpResponse Response = ; HttpServerUtility Server = ; //Specify the output header and encoding = "text/html"; = "utf-8"; HttpFileCollection fs = ; string newFilePath = ["sPath"]; if(>0) { //fs[0] The dirPath corresponding to FindFile is the specified directory, newFilePath is definitely the win svrPath is the target directory, that is, the directory on the server FindFile(fs[0].ToString(), newFilePath); } ("<script>()</script>"); } //Recursively traverse all files in folders and subfiles. public void FindFile(string dirPath,string svrPath) //The parameter dirPath is the specified directory, svrPath is the target directory { //The target directory, that is, the directory on the server string sFilePath = (svrPath); //string sFilePath = (["svrPath"]); //Create a folder if (!(sFilePath)) (sFilePath); //Look for files in the specified directory and subdirectories DirectoryInfo Dir=new DirectoryInfo(dirPath); try { foreach(DirectoryInfo d in ())//Find subdirectories { FindFile(Dir+()+"\\",svrPath+()+"\\"); //FindFile(Dir+()+"\",svrPath+()+"\"); } foreach(FileInfo f in ()) //Find files { //((svrPath + ()));//If you want to save it to another place, please make sure to modify it here ((svrPath + ()), true); ("4554132"); } } catch(Exception e) { ; } } public bool IsReusable { get { return false; } } }
I originally thought that this would achieve the effect, but I discovered a fatal problem! Because the Fileupload control itself does not support uploading of folders, it cannot be assigned to it even through ashx. By learning more information, we know that due to security reasons, it is impossible to upload local folders directly through code on the browser, and they must be achieved through ActiveX controls.
From the analysis of security permissions, it is indeed not allowed. Otherwise, I would write a web page with this js code embedded in it. Once you open this web page, js can start slowly traversing your hard disk and uploading all your files to the server. Uploading is only allowed for files selected by the user through the input control.
This article is just one idea for the editor to solve the problem, not a correct way. The purpose is to learn and communicate with everyone and get better solutions.