WebuploadController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Ant.Service.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  9. {
  10. public class WebuploadController : Controller
  11. {
  12. //
  13. //说明:对于上传大的文件需要在配置文件中改iis配置
  14. //<system.web>
  15. //<httpRuntime maxRequestLength = "999999999" />//用户上传文件最大体积
  16. //< compilation debug="true" targetFramework="4.0" />
  17. //</system.web>
  18. //<system.webServer>
  19. //<security>
  20. //<requestFiltering>
  21. //<requestLimits maxAllowedContentLength = "3000000000" />
  22. //</ requestFiltering >
  23. //</ security >
  24. //</ system.webServer >
  25. public WebuploadController()
  26. {
  27. var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
  28. urlPath = "/PHP";
  29. }
  30. static string urlPath = string.Empty;
  31. static string guid = Guid.NewGuid().ToString("N");
  32. static int num = 1;
  33. public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
  34. {
  35. string filePathName = string.Empty;
  36. string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
  37. if (Request.Files.Count == 0)
  38. {
  39. return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
  40. }
  41. if (!System.IO.Directory.Exists(localPath))
  42. {
  43. System.IO.Directory.CreateDirectory(localPath);
  44. }
  45. if (size > 5242880) //文件大于5M
  46. {
  47. return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "文件不能大于5M" }, id = "id" });
  48. }
  49. string filename = Path.Combine(localPath, file.FileName);
  50. DirFile.DeleteGivenFile(filename);
  51. file.SaveAs(filename);//写入文件夹中
  52. if (DirFile.IsExistFile(filename))
  53. {
  54. return Json(new
  55. {
  56. jsonrpc = "2.0",
  57. id = "id",
  58. filePath = filename
  59. });
  60. }
  61. else
  62. {
  63. return Json(new
  64. {
  65. jsonrpc = "2.0",
  66. id = "id",
  67. filePath = ""
  68. });
  69. }
  70. }
  71. public ActionResult ProcessBak(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
  72. {
  73. string filePathName = string.Empty;
  74. string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
  75. if (Request.Files.Count == 0)
  76. {
  77. return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
  78. }
  79. string ex = Path.GetExtension(file.FileName);
  80. filePathName = guid + "_" + num + ex;//分布接受(建立每个接受分段的名字)
  81. num++;
  82. if (!System.IO.Directory.Exists(localPath))
  83. {
  84. System.IO.Directory.CreateDirectory(localPath);
  85. }
  86. file.SaveAs(Path.Combine(localPath, filePathName));//写入文件夹中
  87. int total = size / 5242880;//判断有多少个分块
  88. if (size % 5242880 > 0)
  89. {
  90. total += 1;
  91. }
  92. //重组
  93. if (num > total)
  94. {
  95. chongzu(total, ex, guid, localPath);
  96. }
  97. return Json(new
  98. {
  99. jsonrpc = "2.0",
  100. id = "id",
  101. filePath = localPath + "/" + filePathName
  102. });
  103. }
  104. //重组(分片数目,接受视频类型,存储视频的名字,存放的地址)
  105. public void chongzu(int total, string ex, string guid, string localPath)
  106. {
  107. try
  108. {
  109. string fileurl = Path.Combine(localPath, guid + ex);
  110. var fs = new FileStream(fileurl, FileMode.Create);
  111. for (int i = 1; i <= total; ++i)
  112. {
  113. string part = Path.Combine(localPath, guid + "_" + i + ex);
  114. var bytes = System.IO.File.ReadAllBytes(part);
  115. fs.Write(bytes, 0, bytes.Length);
  116. bytes = null;
  117. System.IO.File.Delete(part);
  118. }
  119. fs.Close();
  120. string filenameone = guid + ex;
  121. }
  122. catch (Exception e) { Response.Write("<script>alert('视频重组失败') </script>"); }
  123. finally
  124. {
  125. num = 1; guid = Guid.NewGuid().ToString("N"); //为了下个视频重置num 和 guid }
  126. }
  127. }
  128. }
  129. }