日期:2014-05-18 浏览次数:20576 次
string imgpath = "";
Random ran = new Random();
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
DateTime time = DateTime.Now;
string newfilename = time.Year.ToString() + time.Month + time.Day + time.Hour.ToString() + time.Millisecond.ToString() + ran.Next(1000, 9999).ToString() + time.Second.ToString() + time.Millisecond.ToString() + ran.Next(1000, 9999).ToString();
FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string extension = Path.GetExtension(file.Extension);
fileName = newfilename + extension;
imgpath = "uploadfile/" + fileName;
string webFilePath = Server.MapPath("../uploadfile/" + fileName); // 服务器端文件路径
if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath); // 使用
}
catch (Exception ex)
{
Alert.ShowOnly("Tip: file upload fails, the reason for failure:" + ex.Message);
}
}
else
{
Alert.ShowOnly("Tip: file already exists. Please rename after uploading");
}
}
else
{
Alert.ShowOnly("Tip: file types");
}
this.SmallPic.Value = imgpath;
}