|
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
/// <Summary>
/// Upload the file to the specified folder
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnSure_Click (object sender, EventArgs e)
{
// String phName = this.txt Name. Text;
// String phType = this. ddlType. SelectedValue;
If (this. myFile. PostedFile! = Null) // myFile is the name of the upload control
{
String photoName1 = myFile. PostedFile. FileName; // get the initial file name
Int I = photoName1.LastIndexOf ("."); // Retrieve the index of the last "." in the file name
String newext = photoName1.Substring (I); // get the file extension
If (newext! = ". Gif" & newext! = ". Jpg" & newext! = ". Jpeg" & newext! = ". Bmp" & newext! = ". Png ")
{
Response. Write ("The file format is incorrect! ");
Response. End ();
}
DateTime now = DateTime. Now; // Obtain the system time
String classid = DateTime. Now. Year. ToString ();
// Determine whether the current year folder exists under the path based on the year; otherwise, a folder with the year will be created
If (! Directory. exists (HttpContext. current. server. mapPath ("photos/") + "//" + classid) // HttpContext. current. server. mapPath (relative path): converts the relative path to the absolute path on the server. File. Exists (absolute path): Check whether there is a File or directory pointed to by the absolute path.
{
System. IO. directory. createDirectory (@ HttpContext. current. server. mapPath ("photos/") + "//" + classid); // System. IO. directory. createDirectory: create an absolute path folder.
}
String photoName2 = now. millisecond. toString () + "_" + myFile. postedFile. contentLength. toString () + newext; // rename the file, in milliseconds + file size + extension
MyFile. postedFile. saveAs (Server. mapPath ("photos //" + classid + "//" + photoName2); // Save the file to the path and use Server. mapPath () gets the absolute directory of the current file. in asp.net, "/" must be replaced "//"
}
}
/// <Summary>
/// This is an example of creating a folder in a directory specified by the button control, which can be uploaded to different folders by year, month, or by different people
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnCreateBag_Click (object sender, EventArgs e)
{
String classid = DateTime. Now. Year. ToString ();
If (! Directory. exists (HttpContext. current. server. mapPath ("photos/") + "//" + classid) // HttpContext. current. server. mapPath (relative path): converts the relative path to the absolute path on the server. File. Exists (absolute path): Check whether there is a File or directory pointed to by the absolute path.
{
System. IO. directory. createDirectory (@ HttpContext. current. server. mapPath ("photos/") + "//" + classid); // System. IO. directory. createDirectory: create an absolute path folder.
}
Else
{
Response. Write ("<script language = javascript> alert ('folder already exists ') </script> ");
}
}
} |