UploadPhoto.aspx.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. namespace MicroPhotoShare
  7. {
  8. public partial class UploadPhoto : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. if (Request.QueryString["yuname"] != null)
  15. {
  16. string yuname = LYFZ.WinAPI.SDKSecurity.Decode(Request["yuname"].ToString().Trim().Replace(" ", "+"));
  17. string saveName = Request.QueryString["filename"].ToString();
  18. UploadPhotoFile(yuname, saveName);
  19. }
  20. else
  21. {
  22. Response.Write("Error");
  23. }
  24. }
  25. }
  26. private void UploadPhotoFile(string yuname, string saveName)
  27. {
  28. string DirectoryPath = GetUploadPath(yuname);
  29. if (!System.IO.Directory.Exists(DirectoryPath))
  30. {
  31. //System.Security.AccessControl.DirectorySecurity dsec= new System.Security.AccessControl.DirectorySecurity();
  32. System.IO.Directory.CreateDirectory(DirectoryPath);
  33. }
  34. foreach (string f in Request.Files.AllKeys)
  35. {
  36. HttpPostedFile file = Request.Files[f];
  37. if (UploadFile(file, DirectoryPath + "\\" + saveName))
  38. {
  39. Response.Write("Success");
  40. }
  41. else {
  42. Response.Write("Error:" + file.FileName);
  43. }
  44. }
  45. }
  46. public bool ExistsImgage(string fileName)
  47. {
  48. if (fileName.ToLower().EndsWith(".jpg")
  49. || fileName.ToLower().EndsWith(".jpge")
  50. || fileName.ToLower().EndsWith(".png")
  51. || fileName.ToLower().EndsWith(".gif"))
  52. {
  53. return true;
  54. }
  55. else
  56. {
  57. return false;
  58. }
  59. }
  60. public bool UploadFile(HttpPostedFile file, string savePath)
  61. {
  62. try
  63. {
  64. if (ExistsImgage(file.FileName))
  65. {
  66. file.SaveAs(savePath);
  67. return true;
  68. }
  69. else
  70. {
  71. return false;
  72. }
  73. }
  74. catch
  75. {
  76. return false;
  77. }
  78. }
  79. /// <summary>
  80. /// 获取上传路径
  81. /// </summary>
  82. /// <param name="childFolderPath">子目录路径</param>
  83. /// <returns></returns>
  84. public string GetUploadPath(string childFolderPath)
  85. {
  86. return Server.MapPath("~/UploadFolder/" + childFolderPath);
  87. }
  88. }
  89. }