public void SliceUploadFile(HttpContext context)
{
JObject o = new JObject();
context.Response.ContentType = "text/plain";
//如果进行了分片
if (context.Request.Form.AllKeys.Any(m => m == "chunk"))
{
//取得chunk和chunks
int chunk = Convert.ToInt32(context.Request.Form["chunk"]);//当前分片在上传分片中的顺序(从0开始)
int chunks = Convert.ToInt32(context.Request.Form["chunks"]);//总分片数
//根据GUID创建用该GUID命名的临时文件夹
string guid = context.Request["guid"];
string folder = Master_Path + guid + "/";
string path = folder + chunk;
//建立临时传输文件夹
if (!Directory.Exists(Path.GetDirectoryName(folder)))
{
Directory.CreateDirectory(folder);
}
FileStream addFile = new FileStream(path, FileMode.Append, FileAccess.Write);
BinaryWriter AddWriter = new BinaryWriter(addFile);
//获得上传的分片数据流
HttpPostedFile file = context.Request.Files[0];
Stream stream = file.InputStream;
BinaryReader TempReader = new BinaryReader(stream);
//将上传的分片追加到临时文件末尾
AddWriter.Write(TempReader.ReadBytes((int)stream.Length));
//关闭BinaryReader文件阅读器
TempReader.Close();
stream.Close();
AddWriter.Close();
addFile.Close();
TempReader.Dispose();
stream.Dispose();
AddWriter.Dispose();
addFile.Dispose();
if (chunk == chunks - 1)
{
//合并文件
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + Path.GetExtension(file.FileName);
string targetPath = MergeSlice(guid, fileName);
o["filepath"] = JToken.FromObject("SliceUpload/" + DateTime.Now.ToString("yyyyMMdd") + "/" + targetPath);
responseText = ResponseHelper.ResponseSuccess("上传成功", o);
}
else
{
responseText = ResponseHelper.ResponseSuccess("保存成功", o);
}
}
else
{
if (!Directory.Exists(Master_Path))
{
Directory.CreateDirectory(Master_Path);
}
if (!Directory.Exists(Compress_Path))
{
Directory.CreateDirectory(Compress_Path);
}
//没有分片直接保存
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + Path.GetExtension(context.Request.Files[0].FileName);
string targetPath1 = Path.Combine(Master_Path, fileName); //文件的保存路径
//JianJi.BLL.design_DesignDraftHeaderTerritory bll = new JianJi.BLL.design_DesignDraftHeaderTerritory();
//JianJi.Model.design_DesignDraftHeaderTerritory model = new JianJi.Model.design_DesignDraftHeaderTerritory();
//model.TerritoryImgUrl = targetPath;
//model.DesignDraftHeaderID = context.Request.Files[0].DesignDraftHeaderID;
//bll.Add(model);
context.Request.Files[0].SaveAs(targetPath1);
//添加压缩图
ResizeImg.ReSizeImage(targetPath1, Compress_Path);
o["filepath"] = JToken.FromObject("SliceUpload/Compress/" + DateTime.Now.ToString("yyyyMMdd") + "/" + fileName);
responseText = ResponseHelper.ResponseSuccess("上传成功", o);
}
}
private string MergeSlice(string guid, string fileName)
{
HttpContext context = System.Web.HttpContext.Current;
context.Response.ContentType = "text/plain";
string sourcePath = Path.Combine(Master_Path, guid + "/");//源数据文件夹
string targetPath = Path.Combine(Master_Path, fileName);//合并后的文件
DirectoryInfo dicInfo = new DirectoryInfo(sourcePath);
if (Directory.Exists(Path.GetDirectoryName(sourcePath)))
{
FileInfo[] files = dicInfo.GetFiles();
foreach (FileInfo file in files.OrderBy(f => int.Parse(f.Name)))
{
FileStream addFile = new FileStream(targetPath, FileMode.Append);
BinaryWriter AddWriter = new BinaryWriter(addFile);
//获得上传的分片数据流
Stream stream = file.Open(FileMode.Open);
BinaryReader TempReader = new BinaryReader(stream);
//将上传的分片追加到临时文件末尾
AddWriter.Write(TempReader.ReadBytes((int)stream.Length));
//关闭BinaryReader文件阅读器
TempReader.Close();
stream.Close();
AddWriter.Close();
addFile.Close();
TempReader.Dispose();
stream.Dispose();
AddWriter.Dispose();
addFile.Dispose();
}
Directory.Delete(sourcePath, true);//删除
}
return fileName;
}