图片上传怎样防止文件名重名

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;
            }
        }
    }

posted on 2009-11-13 09:43  jay.windows  阅读(980)  评论(0编辑  收藏  举报

导航