//************************************** **************************************** *************
String savepath = server. mappath ("~ /Uploadfile /");
String temp = This. fileupload. filename;
Bool fileok = false;
String newfilename = string. empty;
If (this. fileupload. hasfile)
{
String fileexception = system. Io. Path. getextension (this. fileupload. filename). tolower ();
String [] allowedexception = {". GIF", ". BMP", ". PNG", ". jpg "};
For (INT I = 0; I <allowedexception. length; I ++)
{
If (fileexception = allowedexception [I])
Fileok = true;
}
}
If (fileok) // Image Upload
{
Try
{
Newfilename = tournewshelper. getnewfilename (this. fileupload. filename );
If (file. exists (savepath + newfilename )! = False)
{
This. fileupload. saveas (savepath + newfilename );
}
}
Catch (exception ex)
{
Commonhelper. doupalert (this. Page, "Image Upload Failed! ");
}
}
Else
{
Commonhelper. doupalert (this. Page, "Only GIF, BMP, PNG, and jpg images can be uploaded ");
}
Public static string getnewfilename (string filename)
{
// Generate a new file name consisting of time + random number with the data file name
String newfilename = datetime. Now. year. tostring () + datetime. Now. Month. tostring ()
+ Datetime. Now. Day. tostring () + datetime. Now. Hour. tostring ()
+ Datetime. Now. Second. tostring () + datetime. Now. Minute. tostring ()
+ Datetime. Now. millisecond. tostring ()
// + Rand. Next (1000). tostring ()
+ Filename. substring (filename. lastindexof ("."), filename. Length-filename. lastindexof ("."));
Return newfilename;
}
//************************************** **************************************** **********
The methods to obtain the file type are as follows:
String filecontenttype = fileupload1.postedfile. contenttype;
If (filecontenttype = "image/BMP" | filecontenttype = "image/GIF" | filecontenttype = "image/pjpeg ")
**************************************** *************************************
Private void upfile ()
{
If (uploadfile. postedfile. contentlength> 0 ))
{// Obtain the original name of the uploaded file
String filename =
Uploadfile. postedfile. filename. substring (uploadfile. postedfile. filename. lastindexof ("\\"),
Uploadfile. postedfile. filename. Length-uploadfile. postedfile. filename. lastindexof ("\\"));
/// Obtain the year, month, day, minute, second, and millisecond values of the current time, and combine them into a string in string format
String filetime = datetime. Now. year. tostring () + datetime. Now. Month. tostring ()
+ Datetime. Now. Day. tostring () + datetime. Now. Hour. tostring ()
+ Datetime. Now. Second. tostring () + datetime. Now. Minute. tostring ()
+ Datetime. Now. millisecond. tostring ();
/// Add a random number and file suffix after the time string
Filename = "\" + filetime + getrandomint ()
+ Filename. substring (filename. indexof ("."), filename. Length-filename. indexof ("."));
If (file. exists (server. mappath (request. applicationpath) + "file: // upload/" + filename) = false)
{
/// Upload the file to the server hard disk
Uploadfile. postedfile. saveas (server. mappath (request. applicationpath) + "file: // upload /"
+ Filename );
}
Else
{
Response. Write ("<SCRIPT> alert (\" this file already exists. Please rename your file! \ ") </SCRIPT> ");
}
}
Else
{
Response. Write ("<SCRIPT> alert (\" file name and content cannot be blank! \ ") </SCRIPT> ");
}
}
Private string getrandomint ()
{
Random random = new random ();
Return (random. Next (10000). tostring (); // generates a random positive integer smaller than 10000
}
Bytes ----------------------------------------------------------------------------------------------------
Delete the uploaded file: file. Delete (server. mappath (request. applicationpath) + @ "file: // upload/200583016538151246.gif ");