///////////////// //////////
System. Text. stringbuilder strbder = new system. Text. stringbuilder (); // stringbuilder object to form a string
String fullfilename = This. file1.postedfile. filename; // obtain all the paths and file names of the selected files in the client browser.
String upfilename = fullfilename. substring (fullfilename. lastindexof ("\") + 1); // obtain the file name by taking "\" as the index. The purpose is to obtain the file format
String filetype = upfilename. substring (upfilename. lastindexof (".") + 1); // use "." As the index block to obtain the file type
// The following operations prevent multiple users from simultaneously uploading at the same time, resulting in identical file names and overwriting,
// Avoid using random numbers for a long time. You can increase the random number and continue to reduce the chance of duplicate names.
System. datetime currenttime = new system. datetime (); // creation time object
Currenttime = system. datetime. Now; // you can specify the current time.
Strbder. append (currenttime. year. tostring (); // year
Strbder. append (currenttime. Month. tostring (); // month
Strbder. append (currenttime. Day. tostring (); // day
Strbder. append (currenttime. Hour. tostring (); // hour
Strbder. append (currenttime. Minute. tostring (); // minute
Strbder. append (currenttime. Second. tostring (); // second
Random objrdm = new random (); // create a random object
Strbder. append (objrdm. Next (1,200). tostring (); // generate a random number between 1 -.
Upfilename = strbder. tostring () + "." + filetype; // The name of the file officially created on the server.
Upfilename = upfilename. tolower (); // set all to lower case to avoid inconvenience
Filetype = filetype. tolower (); // It is set to lower case, mainly used to judge
If (filetype = "BMP" | filetype = "jpg" | filetype = "GIF") // accept the type, which can shield the case sensitivity of user extensions.
{
Upfilepos = server. mappath (".. \ upload") + "\" + upfilename;
This. file1.postedfile. saveas (upfilepos); // perform the upload operation
// Response. Write ("<SCRIPT> window. Alert ('added successfully! '); </SCRIPT> "); // prompt that the user is successful.
}
Else
{
Response. Write ("<SCRIPT> window. Alert ('file format error! Only files in BMP, JPG, and GIF formats are accepted '); </SCRIPT> "); // a format error is prompted.
}
///////////////// //////////