using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MicroPhotoShare { public partial class UploadPhoto : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["yuname"] != null) { string yuname = LYFZ.WinAPI.SDKSecurity.Decode(Request["yuname"].ToString().Trim().Replace(" ", "+")); string saveName = Request.QueryString["filename"].ToString(); UploadPhotoFile(yuname, saveName); } else { Response.Write("Error"); } } } private void UploadPhotoFile(string yuname, string saveName) { string DirectoryPath = GetUploadPath(yuname); if (!System.IO.Directory.Exists(DirectoryPath)) { //System.Security.AccessControl.DirectorySecurity dsec= new System.Security.AccessControl.DirectorySecurity(); System.IO.Directory.CreateDirectory(DirectoryPath); } foreach (string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files[f]; if (UploadFile(file, DirectoryPath + "\\" + saveName)) { Response.Write("Success"); } else { Response.Write("Error:" + file.FileName); } } } public bool ExistsImgage(string fileName) { if (fileName.ToLower().EndsWith(".jpg") || fileName.ToLower().EndsWith(".jpge") || fileName.ToLower().EndsWith(".png") || fileName.ToLower().EndsWith(".gif")) { return true; } else { return false; } } public bool UploadFile(HttpPostedFile file, string savePath) { try { if (ExistsImgage(file.FileName)) { file.SaveAs(savePath); return true; } else { return false; } } catch { return false; } } /// /// 获取上传路径 /// /// 子目录路径 /// public string GetUploadPath(string childFolderPath) { return Server.MapPath("~/UploadFolder/" + childFolderPath); } } }