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配置
//
////用户上传文件最大体积
//< compilation debug="true" targetFramework="4.0" />
//
//
//
//
//
// 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(""); }
finally
{
num = 1; guid = Guid.NewGuid().ToString("N"); //为了下个视频重置num 和 guid }
}
}
}
}