C#文件分片上传

本文介绍了如何在ASP.NET中处理HTTP上下文中的文件分片上传,包括接收chunk数据、临时文件操作、合并文件以及压缩处理。方法涉及`SliceUploadFile`和`MergeSlice`函数的实现细节。
摘要由CSDN通过智能技术生成

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;

}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值