123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using Ant.Service.Common;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
- {
- public class WebuploadController : Controller
- {
- //
- //说明:对于上传大的文件需要在配置文件中改iis配置
- //<system.web>
- //<httpRuntime maxRequestLength = "999999999" />//用户上传文件最大体积
- //< compilation debug="true" targetFramework="4.0" />
- //</system.web>
- //<system.webServer>
- //<security>
- //<requestFiltering>
- //<requestLimits maxAllowedContentLength = "3000000000" />
- //</ requestFiltering >
- //</ security >
- //</ system.webServer >
- public WebuploadController()
- {
- var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
- urlPath = "/PHP";
- }
- static string urlPath = string.Empty;
- static string guid = Guid.NewGuid().ToString("N");
- static int num = 1;
- public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
- {
- string filePathName = string.Empty;
- string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
- if (Request.Files.Count == 0)
- {
- return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
- }
- if (!System.IO.Directory.Exists(localPath))
- {
- System.IO.Directory.CreateDirectory(localPath);
- }
- if (size > 5242880) //文件大于5M
- {
- return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "文件不能大于5M" }, id = "id" });
- }
- string filename = Path.Combine(localPath, file.FileName);
- DirFile.DeleteGivenFile(filename);
- file.SaveAs(filename);//写入文件夹中
- if (DirFile.IsExistFile(filename))
- {
- return Json(new
- {
- jsonrpc = "2.0",
- id = "id",
- filePath = filename
- });
- }
- else
- {
- return Json(new
- {
- jsonrpc = "2.0",
- id = "id",
- filePath = ""
- });
- }
- }
- public ActionResult ProcessBak(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
- {
- string filePathName = string.Empty;
- string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
- if (Request.Files.Count == 0)
- {
- return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
- }
- string ex = Path.GetExtension(file.FileName);
- filePathName = guid + "_" + num + ex;//分布接受(建立每个接受分段的名字)
- num++;
- if (!System.IO.Directory.Exists(localPath))
- {
- System.IO.Directory.CreateDirectory(localPath);
- }
- file.SaveAs(Path.Combine(localPath, filePathName));//写入文件夹中
- int total = size / 5242880;//判断有多少个分块
- if (size % 5242880 > 0)
- {
- total += 1;
- }
- //重组
- if (num > total)
- {
- chongzu(total, ex, guid, localPath);
- }
- return Json(new
- {
- jsonrpc = "2.0",
- id = "id",
- filePath = localPath + "/" + filePathName
- });
- }
- //重组(分片数目,接受视频类型,存储视频的名字,存放的地址)
- public void chongzu(int total, string ex, string guid, string localPath)
- {
- try
- {
- string fileurl = Path.Combine(localPath, guid + ex);
- var fs = new FileStream(fileurl, FileMode.Create);
- for (int i = 1; i <= total; ++i)
- {
- string part = Path.Combine(localPath, guid + "_" + i + ex);
- var bytes = System.IO.File.ReadAllBytes(part);
- fs.Write(bytes, 0, bytes.Length);
- bytes = null;
- System.IO.File.Delete(part);
- }
- fs.Close();
- string filenameone = guid + ex;
- }
- catch (Exception e) { Response.Write("<script>alert('视频重组失败') </script>"); }
- finally
- {
- num = 1; guid = Guid.NewGuid().ToString("N"); //为了下个视频重置num 和 guid }
- }
- }
- }
- }
|