mulUploadImg.aspx.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using Utils;
  10. using Utils.ImageUtils;
  11. public partial class mulUploadImg : System.Web.UI.Page
  12. {
  13. public static string dPath = ConfigurationManager.AppSettings["dPath"];
  14. private void conSuc(string msg)
  15. {
  16. Response.Write("{\"res\":\"1\",\"msg\":\"" + msg + "\"}");
  17. //Response.End();
  18. }
  19. private void conErc(string msg)
  20. {
  21. Response.Write("{\"res\":\"0\",\"msg\":\"" + msg + "\"}");
  22. //Response.End();
  23. }
  24. public static string GenerateStringID()
  25. {
  26. long i = 1;
  27. foreach (byte b in Guid.NewGuid().ToByteArray())
  28. {
  29. i *= ((int)b + 1);
  30. }
  31. return string.Format("{0:x}", i - DateTime.Now.Ticks);
  32. }
  33. protected void Page_Load(object sender, EventArgs e)
  34. {
  35. if (Request.Files.Count < 1)
  36. {
  37. conErc("空文件!");
  38. return;
  39. }
  40. try
  41. {
  42. string ut = Request.QueryString["ut"];
  43. HttpPostedFile postFile = Request.Files[0];
  44. if (postFile != null)
  45. {
  46. string errMsg = "";
  47. if (ut != "fp")
  48. {
  49. if (!CheckImage(postFile, out errMsg))
  50. {
  51. Response.Write("{\"res\":\"0\",\"msg\":\"不是有效的图片文件!\"}");
  52. Response.End();
  53. return;
  54. }
  55. }
  56. //using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(postFile.InputStream))
  57. //{
  58. // result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 400, "", 9, 3);
  59. //}
  60. string sPath = Path.Combine(dPath, ut);
  61. if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
  62. string fName = GenerateStringID() + Path.GetExtension(postFile.FileName);
  63. string saveFile = Path.Combine(sPath, fName);
  64. //上传文件
  65. postFile.SaveAs(saveFile);
  66. //conSuc(fName);
  67. Response.Write("{\"res\":\"1\",\"msg\":\"上传成功!\",\"fn\":\"" + fName + "\"}");
  68. return;
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. conErc("发生错误!" + CommonHelper.FormatTextArea(ex.Message));
  74. return;
  75. }
  76. conErc("无法上传");
  77. }
  78. public static bool CheckImage(HttpPostedFile postedFile, out string errMsg)
  79. {
  80. errMsg = "";
  81. if (postedFile == null || postedFile.FileName == "")
  82. {
  83. errMsg = "请选择要上传的图片";
  84. return false;
  85. }
  86. if (postedFile.ContentLength > 83886080)
  87. {
  88. errMsg = "上传的图片大小不允许超过80MB";
  89. return false;
  90. }
  91. return true;
  92. }
  93. }