/// <summary> /// 上传图片 /// </summary> /// <param name="fileupload">上传的控件</param> /// <param name="folder">要存储的文件夹(要在服务器已经存在)</param> /// <param name="imageName">图片名称</param> /// <returns></returns> public string UploadImage(FileUpload fileupload, string folder, string imageName) { Boolean fileOK = false; String path = Server.MapPath("~/" + folder + "/"); if (fileupload.HasFile) { String fileExtension = System.IO.Path.GetExtension(fileupload.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } else { return "请选择要上传的文件!"; } if (fileOK) { try { string ImagesPath = path + imageName + Path.GetExtension(fileupload.FileName); fileupload.PostedFile.SaveAs(ImagesPath); return "上传成功!"; } catch { return "上传失败!"; } } else { return "只能上传 .gif, .png, .jpeg, .jpg 等类型图片!"; } }