public void UploadImg(string folderName) { if (!FileUpload1.HasFile) return; string extendName = Path.GetExtension(FileUpload1.PostedFile.FileName); string[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; if (!fileType.Contains(extendName)) { ScriptManager.RegisterClientScriptBlock(btn_FileUp, btn_FileUp.GetType(), "script", "alert('上传图片格式不正确');", true); } else { string fName = string.Format("{0:yyyyMM}", DateTime.Now); if (!Directory.Exists(Server.MapPath(folderName))) { if (folderName != null) Directory.CreateDirectory(string.Format("~/sc/{0}/{1}/",fName,Server.MapPath(folderName))); } string newPath = Server.MapPath(folderName) + DateTime.Now.ToString("yyyyMMddhhmmss")+extendName; FileUpload1.PostedFile.SaveAs(newPath); //这里可以给图片控件赋值,给隐藏字段赋值 } }
更新于2018年12月25日