调用:UpLoadIMG(this.FileName, "/upload/pic/", ".GIF.JPG.PNG.SWF");
public static string TransToTwo(string theStr)
{
if (theStr.Length == 1)
{
return "0"+theStr;
}
else
{
return theStr;
}
}
public static string UpLoadIMG(FileUpload fulFile, string DirectionName,string ExtName)
{
try
{
//string FileName = fulFile.FileName;
string returnStr = "";
string FileSavePath = System.Web.HttpContext.Current.Server.MapPath("~/"); //获取文件存放的根路径
if (fulFile.HasFile) //判断是否选择了要上传的文件
{
string name = fulFile.PostedFile.FileName;
string DirectionName2 = DateTime.Now.Year.ToString() + TransToTwo(DateTime.Now.Month.ToString());
string DirectionName3 = TransToTwo(DateTime.Now.Day.ToString()) + TransToTwo(DateTime.Now.Hour.ToString()) + TransToTwo(DateTime.Now.Minute.ToString()) + TransToTwo(DateTime.Now.Second.ToString()) + DateTime.Now.Millisecond.ToString();
FileInfo file = new FileInfo(name);
string CreatePath="";
CreatePath =FileSavePath + DirectionName + DirectionName2 ;
FileSavePath += DirectionName + DirectionName2 +"/"+DirectionName3+ file.Extension;
if (ExtName.Contains( file.Extension.ToUpper() )) //符合类型
{
try
{
try
{
if (!Directory.Exists(CreatePath))
{
Directory.CreateDirectory(CreatePath);
}
}
catch
{
throw;
}
fulFile.SaveAs(FileSavePath);
}
catch (Exception Error)
{
HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>");
return "";
}
returnStr = DirectionName2 + "/" + DirectionName3;
}
else
{
HttpContext.Current.Response.Write(" <script>alert('该文件格式不正确!') </script>");
return "";
}
}
else
{
return "";
}
}
catch
{
return "";
}
}