防止文件重名方法

protected void btnUpload_Click(object sender, EventArgs e)
{
if (upload1.HasFile)
{
HttpPostedFile file
= upload1.PostedFile;
string extension = Path.GetExtension(file.FileName);
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + extension;
string path = Server.MapPath("uploads");
string savePath = Path.Combine(path, fileName);
//Response.Write(savePath);
string imagePath = "uploads/" + fileName;
file.SaveAs(savePath);
if (extension.Equals(".bmp") || extension.Equals(".jpg") || extension.Equals(".gif"))
{
Image1.ImageUrl
= imagePath;
}
}
}

1.重新命名成yyyyMMddhhmmss+后缀名的文件就行了。
2.Guid
3.yyyyMMddhhmmss+原文件名
posted @ 2009-09-29 09:22  sig556  阅读(396)  评论(0编辑  收藏  举报