代码
/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnPro_Click(object sender, EventArgs e) { if (Session["User"] != null) { try { string Imgfile = string.Empty; if (FileUpload_ProductImg.HasFile) { string path = Server.MapPath("~/mImages/ProductUploadFile/"); Imgfile = "mImages/ProductUploadFile/"; //拼接文件的名称 //DirectoryInfo dir = new DirectoryInfo(path); //文件名由文件夹中文件的数量和后缀组成 //string name = dir.GetFiles().Length + Path.GetExtension(FileUpload_ProductImg.FileName); string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + Path.GetExtension(FileUpload_ProductImg.FileName); Imgfile = Imgfile + name; FileUpload_ProductImg.PostedFile.SaveAs(path + name); }else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", @"<script>alert('没有选择文件,请先选择文件!');location.href='NewsContentList.aspx';</script>"); return; } } catch (Exception) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", @"<script>alert('上传失败,请联系管理员!');location.href='NewsContentList.aspx';</script>"); return; } } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", @"<script>alert('登入超时!');location.href='Login.aspx';</script>"); return; } }