filePath+=DateTime.Now.Minute.ToString("00");
filePath+=DateTime.Now.Second.ToString("00");
filePath+=DateTime.Now.Millisecond.ToString("00");
filePath+=rd.Next(99).ToString("00");
filePath+="."+fext;
return filePath;
}
private string SaveToServer(byte[] data,string fext,string physicPath,int fileLen)
{
string filePath=CreateFilePath(fext);
string rtnValue=filePath;
filePath=filePath.Insert(0,@physicPath);
if(File.Exists(filePath))
{
filePath=CreateFilePath(fext);
rtnValue=filePath;
}
FileStream fs=new FileStream(filePath,FileMode.CreateNew);
fs.Write(data,0,fileLen);
fs.Close();
return rtnValue;
}
//在其他页面调用该上传类,见下面的实例:
UpLoadAndSaveImage upload=new UpLoadAndSaveImage();
try
{
string virPath="UploadFiles/";
string physicPath=Server.MapPath(Request.ApplicationPath+"/"+"UploadFiles/");
string fext=this.File1.PostedFile.FileName;
if(fext.Length==0)
{
return;
}
fext=Path.GetExtension(fext).ToLower();
if(fext!=".jpg"&&fext!=".gif"&&fext!=".bmp"&&fext!=".doc"&&fext!=".rar"&&fext!
=".zip"&&fext!=".jpeg")
{
Response.Write("<script>alert('Invalid file format,the file format must be jpg or jpeg or gif
or bmp or doc or rar or zip')</script>");
return;
}