I want to share my ideas with you. I don't know if you have a better solution.
Source code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// MyBlog: http://seay.sinaapp.com/
}
Protected void button#click (object sender, EventArgs e)
{
# Region file type determination
// Get the Upload File Name
String FileName = FileUpload1.FileName;
// Determine whether the file name contains wood.
If (! (FileName. Contains (".")))
{
Response. Write ("this file type cannot be uploaded! ");
Return;
}
// Obtain the subscript.
Int index = FileName. LastIndexOf ('.');
Char [] c = FileName. ToCharArray ();
String File_hz = "";
// Get the Suffix in a loop
For (int I = 0; I <FileName. Length-index; I ++)
{
File_hz + = c [index + I];
}
// File name that can be uploaded
String [] FileType = {". jpg", ". gif", ". bmp", ". jpeg "};
Bool bl = false;
// Cyclically traverse whether the uploaded file extension is in the allowed Extension
Foreach (string str in FileType)
{
If (str = File_hz)
{
Bl = true;
}
}
If (bl = false)
{
Response. Write ("this file type cannot be uploaded! ");
Return;
}
# Endregion
// Get the timestamp and name it in the file. This makes writing a bit complicated. I don't know if you have a good way to get the timestamp.
DateTime starttime = TimeZone. CurrentTimeZone. ToLocalTime (new System. DateTime (1970, 1, 1, 0, 0, 0, 0 ));
DateTime newtime = DateTime. Now;
Long utime = (long) Math. Round (newtime-starttime). TotalMilliseconds, MidpointRounding. AwayFromZero );
FileName = utime. ToString () + File_hz;
// Obtain the number of file bytes
String FileLenght = FileUpload1.PostedFile. ContentLength. ToString ();
String FilePath = Server. MapPath ("img/" + FileName );
// Upload
FileUpload1.SaveAs (FilePath );
Response. Write ("uploaded successfully <br/> file size:" + FileLenght + "<br/> path: img/" + FileName );
}
}