The previous BS folder upload operation illustrates my needs,
Next, the last proposition:
"If you have a requirement, do you need to implement the folder upload function on BS? How do you implement it ?"
ActiveX? JS plug-in? Custom controls or ......
Let's look at the following requirements:
The client uploads any folder to the server, creates a folder on the server, and uploads the corresponding file to the corresponding folder.
Operation: select any folder to upload related files in the folder.
Results:
Main source code:
/// <Summary>
/// Import-> confirm Method
/// </Summary>
/// <Param name = "path"> </param>
[Directmethod]
Public Void Getpath ( String Readpath)
{
String Storepath = @" C: \ Documents and Settings \ Administrator \ Desktop \ test \ " ; // Target directory
String Strfile = folderhelper. findfilebypath (readpath, storepath );
Commonmethod. Show ( "" , Strfile, ext. net. MessageBox. Icon. info );
}
Folderhelper
/// <Summary>
/// Obtain files in this folder by path
/// </Summary>
/// <Param name = "readpath"> File Read path </Param>
/// <Param name = "storepath"> File Storage path </Param>
/// <Returns> Import successful, failed </Returns>
Public Static String Findfilebypath ( String Readpath, String Storepath)
{
Try
{
Directoryinfo diinfo = New Directoryinfo (readpath ); // Upload
Directoryinfo diinfostore = New Directoryinfo (storepath + diinfo. Name ); // Storage
If (! Diinfostore. exists) // Stored in the specified path to determine that the folder does not exist.
Diinfostore. Create ();
Filesysteminfo [] fsinfo = diinfo. getfilesysteminfos (); // Obtain all files and subdirectories in this directory
Foreach (Filesysteminfo FS In Fsinfo) // Traverse Arrays
{
If (FS Is Fileinfo) // File
{
Byte [] File = filetobinary (FS. fullname );
Binarytofile (storepath + diinfo. Name, FS. Name, file );
}
If (FS Is Directoryinfo) // Folder
{
Findfilebypath (FS. fullname, storepath + diinfo. Name + " \\ " );
}
}
Return " Imported " ;
}
Catch (Exception)
{
Return " Import failed " ;
}
}
/// <Summary>
/// Convert a file to a binary stream for reading
/// </Summary>
/// <Param name = "FILENAME"> Complete file name path </Param>
/// <Returns> File binary stream </Returns>
Private Static Byte [] Filetobinary ( String Filename)
{
Filestream fsread = New Filestream (filename, filemode. Open, fileaccess. Read );
Try
{
If (Fsread. Canread)
{
Int Fssize = convert. toint32 (fsread. Length );
Byte [] Btread = New Byte [Fssize];
Fsread. Read (btread, 0 , Fssize );
Return Btread;
}
Else
{
Throw New Exception ( " File Reading error! " );
}
}
Catch (Exception ex)
{
Throw New Exception (ex. Message );
}
Finally
{
Fsread. Close ();
}
}
/// <Summary>
/// Convert binary data to the corresponding file for storage
/// </Summary>
/// <Param name = "storepath"> Path for storing file names </Param>
/// <Param name = "FILENAME"> File Name </Param>
/// <Param name = "btbinary"> Binary stream </Param>
Private Static Void Binarytofile ( String Storepath, String Filename, Byte [] Binary)
{
Filestream fswrite =New Filestream (storepath + " \\ " + Filename, filemode. Create, fileaccess. Write );
Try
{
If (Fswrite. canwrite)
{
Fswrite. Write (binary, 0 , Binary. Length );
}
}
Catch (Exception ex)
{
Throw New Exception (ex. Message );
}
Finally
{
Fswrite. Close ();
}
}
The basic upload function is implemented, but at the cost of security, you can try the Help ActiveX provided by left join. G. Thanks to the left join. G and sunriseyuen of the two friends in the garden.
Thank you.
Author: Pepe
Source: http://pepe.cnblogs.com/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.