地铁二期项目正式开始

UploadController.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. using YTSoft.BaseCallCenter.Model;
  11. using YTSoft.BaseCallCenter.MVCWeb.Models;
  12. /// <summary>
  13. /// 上传文件
  14. /// </summary>
  15. namespace YTSoft.BaseCallCenter.MVCWeb.Controllers
  16. {
  17. public class UploadController : BaseController
  18. {
  19. /// <summary>
  20. /// 文件上传
  21. /// </summary>
  22. /// <returns></returns>
  23. [AcceptVerbs(HttpVerbs.Post)]
  24. public ActionResult UploadFile()
  25. {
  26. string[] filesuffix = ConfigurationManager.AppSettings["filesuffix"].Split(';');
  27. try
  28. {
  29. if (Request.Files.Count > 0)
  30. {
  31. string uppath = string.Empty;
  32. string savepath = string.Empty;
  33. HttpPostedFileBase OrderFile = Request.Files[0];
  34. if (OrderFile != null)
  35. {
  36. //创建文件新的名称
  37. string nameImg = F_UserCode+DateTime.Now.ToString("yyyyMMddHHmmssfff");
  38. //获得上传文件的路径
  39. string strPath = OrderFile.FileName;
  40. //获得上传文件的类型(后缀名)
  41. string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower();
  42. if (!filesuffix.Contains(type))
  43. {
  44. return Json(new
  45. {
  46. code = 1,
  47. src = "",
  48. msg = "不支持格式"
  49. });
  50. }
  51. int byteCount = OrderFile.ContentLength;
  52. if (byteCount > 52428800)
  53. {
  54. return Json(new
  55. {
  56. code = 1,
  57. src = "",
  58. msg = "文件不能大于50M"
  59. });
  60. }
  61. //拼写数据库保存的相对路径字符串
  62. savepath = "/Content/Upload/orderfile/";
  63. //拼写上传图片的路径
  64. uppath = Server.MapPath("~/Content/Upload/orderfile/");
  65. if (!Directory.Exists(Path.GetDirectoryName(uppath)))
  66. {
  67. Directory.CreateDirectory(Path.GetDirectoryName(uppath));
  68. }
  69. uppath += nameImg + "." + type;
  70. savepath += nameImg + "." + type;
  71. //上传图片
  72. OrderFile.SaveAs(uppath); //原图片路径
  73. }
  74. return Json(new
  75. {
  76. code = 0,
  77. src = savepath,
  78. msg = "上传成功"
  79. });
  80. }
  81. else
  82. {
  83. return Json(new
  84. {
  85. code = 1,
  86. src = "",
  87. msg = "上传出错 请检查文件名称或文件大小"
  88. });
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. return Json(new
  94. {
  95. code = 1,
  96. src = "",
  97. msg = "上传出错: " + ex.Message
  98. });
  99. }
  100. }
  101. }
  102. }