simUploadFile.aspx.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 simUploadFile : 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. protected void Page_Load(object sender, EventArgs e)
  25. {
  26. if (Request.Files.Count < 1)
  27. {
  28. conErc("空文件!");
  29. return;
  30. }
  31. try
  32. {
  33. string ut = Request.QueryString["ut"];
  34. HttpPostedFile postFile = Request.Files[0];
  35. if (postFile != null)
  36. {
  37. string errMsg = "";
  38. if (ut != "fp")
  39. {
  40. if (!ImageHandler.CheckImage(postFile, out errMsg))
  41. {
  42. Response.Write("{\"res\":\"0\",\"msg\":\"不是有效的图片文件!\"}");
  43. Response.End();
  44. return;
  45. }
  46. }
  47. //using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(postFile.InputStream))
  48. //{
  49. // result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 400, "", 9, 3);
  50. //}
  51. string strMonth = DateTime.Now.ToString("yyyyMM");
  52. string sPath = Path.Combine(dPath, ut, strMonth);
  53. if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
  54. string fName = DateTime.Now.ToFileTime().ToString() + Path.GetExtension(postFile.FileName);
  55. string saveFile = Path.Combine(sPath, fName);
  56. //上传文件
  57. postFile.SaveAs(saveFile);
  58. conSuc(fName);
  59. return;
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. conErc("发生错误!" + CommonHelper.FormatTextArea(ex.Message));
  65. return;
  66. }
  67. conErc("无法上传");
  68. }
  69. }