首先是前端:

 var GUID = WebUploader.Base.guid();//一个GUID
uploadereditsVideo = WebUploader.create({
// swf文件路径
swf: '../../Scripts/plugins/webuploader/Uploader.swf',
// 文件接收服务端。
server: '/AjaxUpload/ChunkUpload',
pick: {
id: '#picker',
label: '上传',
innerHTML: '上传',
multiple: false
},
fileNumLimit: 1,
fileSingleSizeLimit: 1024 * 1024 *100,
chunked: true,//开始分片上传
chunkSize: 1024 * 1024 * 2,//每一片的大小
formData: {
guid: GUID //自定义参数
}
}); uploadereditsVideo.on('fileQueued', function (file) {
uploadereditsVideo.upload();
});
// 文件上传成功
uploadereditsVideo.on('uploadSuccess', function (file, response) {
//合并文件
$.post('/AjaxUpload/Merge', { guid: GUID, fileName: file.name }, function (data) {
if (data.r == 1) {
alert("上传完成");
}
else {
alert(data.err);
}
});
});

后端接收方法Upload

 public ActionResult Upload()
{
//如果进行了分片
if (Request.Form.AllKeys.Any(m => m == "chunk"))
{
//取得chunk和chunks
int chunk = Convert.ToInt32(Request.Form["chunk"]);//当前分片在上传分片中的顺序(从0开始)
int chunks = Convert.ToInt32(Request.Form["chunks"]);//总分片数
//根据GUID创建用该GUID命名的临时文件夹
string folder = Server.MapPath("~/upload/" + Request["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);
//获得上传的分片数据流
var file = Request.Files[];
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();
return Json(new { chunked = true, hasError = false, f_ext = Path.GetExtension(file.FileName) });
}
else//没有分片直接保存
{
Request.Files[].SaveAs(Server.MapPath("~/upload/" + DateTime.Now.ToFileTime() + Path.GetExtension(Request.Files[].FileName)));
return Json(new { chunked = true, hasError = false });
}
}

合并方法Merge

public ActionResult Merge()
{
try
{
var guid = Request["guid"];//GUID
var uploadDir = Server.MapPath("~/upload");//Upload 文件夹
var dir = Path.Combine(uploadDir, guid);//临时文件夹
var ext = Path.GetExtension(Request["fileName"]);
var files = Directory.GetFiles(dir);//获得下面的所有文件
var name = Guid.NewGuid().ToString("N") + ext;
var finalPath = Path.Combine(uploadDir, name);//最终的文件名
var fs = new FileStream(finalPath, FileMode.Create);
foreach (var part in files.OrderBy(x => x.Length).ThenBy(x => x))//排一下序,保证从0-N Write
{
var bytes = System.IO.File.ReadAllBytes(part);
fs.Write(bytes, , bytes.Length);
bytes = null;
System.IO.File.Delete(part);//删除分块
}
fs.Flush();
fs.Close();
Directory.Delete(dir);//删除文件夹
return Json(new { r = , path = "/upload/" + name });
}
catch (Exception ex)
{
return Json(new { r = , err = ex.Message });
}
}

以上就是分片上传的方法

asp.net mvc+webuploader大文件分片上传的更多相关文章

  1. Webuploader 大文件分片上传

    百度Webuploader 大文件分片上传(.net接收)   前阵子要做个大文件上传的功能,找来找去发现Webuploader还不错,关于她的介绍我就不再赘述. 动手前,在园子里找到了一篇不错的分片 ...

  2. 百度Webuploader 大文件分片上传(.net接收)

    前阵子要做个大文件上传的功能,找来找去发现Webuploader还不错,关于她的介绍我就不再赘述. 动手前,在园子里找到了一篇不错的分片上传的帖子,参考之后,踏出了第一步.此文记录我这次实践的点滴,仅 ...

  3. 百度Webuploader 大文件分片上传(.net接收)

    版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  4. asp.net实现浏览器大文件分片上传

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  5. thinkphp+webuploader实现大文件分片上传

    大文件分片上传,简单来说就是把大文件切分为小文件,然后再一个一个的上传,到最后由这些小文件再合并成原来的文件 webuploader下载地址及其文档:http://fex.baidu.com/webu ...

  6. 在React中使用WebUploader实现大文件分片上传的踩坑日记!

    前段时间公司项目有个大文件分片上传的需求,项目是用React写的,大文件分片上传这个功能使用了WebUploader这个组件. 具体交互是: 1. 点击上传文件button后出现弹窗,弹窗内有选择文件 ...

  7. Vue2.0结合webuploader实现文件分片上传

    Vue项目中遇到了大文件分片上传的问题,之前用过webuploader,索性就把Vue2.0与webuploader结合起来使用,封装了一个vue的上传组件,使用起来也比较舒爽. 上传就上传吧,为什么 ...

  8. java springboot 大文件分片上传处理

    参考自:https://blog.csdn.net/u014150463/article/details/74044467 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时 ...

  9. vue+大文件分片上传

    最近公司在使用vue做工程项目,实现大文件分片上传. 网上找了一天,发现网上很多代码都存在很多问题,最后终于找到了一个符合要求的项目. 工程如下: 对项目的大文件上传功能做出分析,怎么实现大文件分片上 ...

随机推荐

  1. 查看win激活状态的命令

    查看win激活状态的命令  1.键盘按下win+R 运行输入如下命令即可. 2.Win+R===>输入 slmgr.vbs -dlv   显示:最为详尽的激活信息,包括:激活ID.安装ID.激活 ...

  2. 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)

    转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootbanmybatisnixiangshengchenggongju32/ ...

  3. map的访问

    映射表(map) 在每个条目被插入时将之按键进行排序.取迭代器指向值时将返回value_type结构,它有两个数据成员:first,second.访问first获得键的数据,访问second获得值的数 ...

  4. 【306】通过ArcPy编写ArcToolbox

    参考:使用 Python 创建工具 参考:在 Python 工具箱中定义参数数据类型 基本步骤如下: (1)创建一个 Python 脚本,并保存成 .py 文件. (2)创建一个自定义工具箱(.tbx ...

  5. cannot import name '_validate_lengths' from 'numpy.lib.arraypad'

    在Anaconda下新配置了tensorflow环境,结果在引入skimage 包时报错,错误提示from numpy.lib.arraypad import _validate_lengths,找不 ...

  6. 653. Two Sum IV - Input is a BST 二叉树版本

    [抄题]: Given a Binary Search Tree and a target number, return true if there exist two elements in the ...

  7. ORACLE修改表空间方法

    一.使用imp/exp.先导出源库,再创建新库把表空间创建好,然后再导入.(据说这样可以,前提是新的库里面不能有与源库相同名字的表空间.有待验证!) 二.使用脚本进行修改.据目前所了解,正长情况下需要 ...

  8. Springboot-读取核心配置文件及自定义配置文件

    读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 核心 ...

  9. [SoapUI] SoapUI命令行方式运行

    https://www.soapui.org/test-automation/running-from-command-line/functional-tests.html TestRunner Co ...

  10. Mosquitto 单向SSL配置

    Mosquitto 单向SSL配置 摘自:https://blog.csdn.net/a_bcd_123/article/details/70167833 2017年04月14日 06:56:06 s ...