asp.net core 分块上传

  public class HomeController : Controller
    {
 
        private IHostingEnvironment hostingEnv;
        public HomeController( IHostingEnvironment env)
        {
        
            this.hostingEnv = env;
        }
        public IActionResult Index()
        {
         
            return View();
        }
        [HttpPost]
        public JsonResult uplode(string fileName, string fileSize, string uuid, string chunked,IFormFile data ,string currChunk, string totalChunk)
        {
            var fid = uuid + currChunk.ToString();
            var ext = data.FileName.Split('.').Last();
            var filename = fid + "." + ext;
            string path = hostingEnv.WebRootPath + $@"\{uuid}";
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);             
            }
            filename = hostingEnv.WebRootPath + $@"\{uuid}\{filename}";
            using (FileStream fs = System.IO.File.Create(filename))
            {
                data.CopyTo(fs);
                fs.Flush();
            }
            return Json("");
        }
        public JsonResult fileSize(string uuid)
        {
            string path = hostingEnv.WebRootPath + $@"\{uuid}";
            long len = 0;
            //定义一个DirectoryInfo对象
            DirectoryInfo di = new DirectoryInfo(path);
            //通过GetFiles方法,获取di目录中的所有文件的大小
            foreach (FileInfo fi in di.GetFiles())
            {
                len += fi.Length;
            }
            //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
           
            return Json(len);
        }
        public JsonResult isHave(string fileName)
        {
            string path = hostingEnv.WebRootPath + $@"\{fileName}";
            if (System.IO.File.Exists(path))
            {
                //存在文件
                System.IO.File.Delete(path);
                return Json("1");

            }
            else
            {
                return Json("0");
            }
        }
        public JsonResult hebing(string uuid,int chunks,string fileName)
        {
            var newpath = hostingEnv.WebRootPath + $@"\{fileName}";
            var path = hostingEnv.WebRootPath + $@"\{uuid}\";
            var folderPath = hostingEnv.WebRootPath + $@"\{uuid}";
            FileStream AddStream = null;
            //以合并后的文件名称和打开方式来创建、初始化FileStream文件流
            AddStream = new FileStream(newpath, FileMode.Append);
            //以FileStream文件流来初始化BinaryWriter书写器,此用以合并分割的文件
            BinaryWriter AddWriter = new BinaryWriter(AddStream);
            FileStream TempStream = null;
            BinaryReader TempReader = null;
            //循环合并小文件,并生成合并文件
            for (int i = 0; i < chunks; i++)
            {
                //以小文件所对应的文件名称和打开模式来初始化FileStream文件流,起读取分割作用
                TempStream = new FileStream(path + uuid + i.ToString() + ".blob", FileMode.Open);
                TempReader = new BinaryReader(TempStream);
                //读取分割文件中的数据,并生成合并后文件
                AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length));
                //关闭BinaryReader文件阅读器
                TempReader.Dispose();
                //关闭FileStream文件流
                TempStream.Dispose();
              

            }
            //关闭BinaryWriter文件书写器
            AddWriter.Dispose();
            //关闭FileStream文件流
            AddStream.Dispose();
            Directory.Delete(folderPath, true);
            return Json("1");
        }
    }
前台代码

 

<input type="file" multiple="multiple" id="file" οnchange="showFileList(this.files)" />
<input id="uploadBtn" type="button" value="上传" οnclick="doUpload()" />&nbsp;



<table>
<tr>
<td><div style="width:1000px;height:40px; ">
<div id="d" style="background-color:aqua;width:1px;height:40px;"></div>
</div></td><td><div id="l"></div></td>
</tr>
</table>

<script>

function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
var quence = new Array();//待上传的文件队列,包含切块的文件
var uuid = "static" + guid();
var chunkSize = 5 * 1024 * 1024; //切块的阀值:5M

/**
* 用户选择文件之后的响应函数,将文件信息展示在页面,同时对大文件的切块大小、块的起止进行计算、入列等
*/
function showFileList(files) {
if(!files) {
return;
}


$(files).each(function(idx,e){
//展示文件列表,略......

if(e.size > chunkSize) {//文件大于阀值,进行切块
//切块发送
var chunks = Math.max(Math.floor(e.size / chunkSize), 1) + 1;//分割块数
for(var i=0 ; i<chunks; i++) {
var startIdx = i*chunkSize;//块的起始位置
var endIdx = startIdx+chunkSize;//块的结束位置
if (endIdx > e.size) {
endIdx = e.size;
}
var lastChunk = false;
if(i == (chunks-1)) {
lastChunk = true;
}
//封装成一个task,入列
var task = {
file:e,
uuid:uuid,//避免文件的重名导致服务端无法定位文件,需要给每个文件生产一个UUID
chunked:true,
startIdx:startIdx,
endIdx:endIdx,
currChunk:i,
totalChunk:chunks
}
quence.push(task);

}
} else {//文件小于阀值

var task = {
file:e,
uuid:uuid,
chunked:false
}
quence.push(task);

}
});
}

/**
* 上传器,绑定一个XMLHttpRequest对象,处理分配给其的上传任务
**/
function Uploader(name) {
this.url = "/home/uplode"; //服务端处理url
this.req = new XMLHttpRequest();
this.tasks; //任务队列
this.taskIdx = 0; //当前处理的tasks的下标
this.name=name;
this.status=0; //状态,0:初始;1:所有任务成功;2:异常

//上传 动作
this.upload = function(uploader) {
this.req.responseType = "json";

//注册load事件(即一次异步请求收到服务端的响应)
this.req.addEventListener("load", function () {

//更新对应的进度条

//从任务队列中取一个再次发送
var task = uploader.tasks[uploader.taskIdx];
if(task) {
console.log(uploader.name + ":当前执行的任务编号:" +uploader.taskIdx);
this.open("POST", uploader.url);
this.send(uploader.buildFormData(task));
uploader.taskIdx++;
} else {
console.log("处理完毕");
uploader.status=1;
}
});

//处理第一个
var task = this.tasks[this.taskIdx];
if(task) {
console.log(uploader.name + ":当前执行的任务编号:" +this.taskIdx);
this.req.open("POST", this.url);
this.req.send(this.buildFormData(task));
this.taskIdx++;
} else {
uploader.status=1;
}
}

//提交任务
this.submit = function(tasks) {
this.tasks = tasks;
}

//构造表单数据
this.buildFormData = function(task) {
var file = task.file;
var formData = new FormData();
formData.append("fileName", file.name);
formData.append("fileSize", file.size);
formData.append("uuid", task.uuid);
var chunked = task.chunked;
if(chunked) {//分块
formData.append("chunked", task.chunked);
formData.append("data", file.slice(task.startIdx, task.endIdx));//截取文件块
formData.append("currChunk", task.currChunk);
formData.append("totalChunk", task.totalChunk);
} else {
formData.append("data", file);
}
return formData;
}

}
function uploadComplete()
{
var fileObj = document.getElementById('file').files[0]; // 获取文件对象
var size = fileObj.size;
var fileName = fileObj.name;
var chunks = Math.max(Math.floor(fileObj.size / chunkSize), 1) + 1;//分割块数
$.ajax({
url: "/home/fileSize?uuid="+uuid,
dataType: "json",
success: function (list) {
console.log(list)
var result = (list / size * 100).toFixed(2);
var width = result * 10;
$("#d").css("width", width + "px");
$("#l").html(result+"%")
if (result == "100.00") {
$.get("/HOME/hebing?uuid=" + uuid + "&chunks=" + chunks + "&fileName=" + fileName, "", function () {
window.location.reload();
})
}

}

})
}


function doUpload() {
//创建4个Uploader上传器(4条线程)
var uploader0 = new Uploader("uploader0");
var task0 = new Array();

var uploader1 = new Uploader("uploader1");
var task1 = new Array();

var uploader2 = new Uploader("uploader2");
var task2 = new Array();

var uploader3 = new Uploader("uploader3");
var task3 = new Array();

//将文件列表取模hash,分配给4个上传器
for(var i=0 ; i<quence.length; i++) {
if(i%4==0) {
task0.push(quence[i]);
} else if(i%4==1) {
task1.push(quence[i]);
} else if(i%4==2) {
task2.push(quence[i]);
} else if(i%4==3) {
task3.push(quence[i]);
}
}
//提交任务,启动线程上传
uploader0.submit(task0);
uploader0.upload(uploader0);
uploader1.submit(task1);
uploader1.upload(uploader1);
uploader2.submit(task2);
uploader2.upload(uploader2);
uploader3.submit(task3);
uploader3.upload(uploader3);
//注册一个定时任务,每2秒监控文件是否都上传完毕
setInterval(“uploadComplete()”, 2000)
}
</script>

  

转载于:https://www.cnblogs.com/cainiaoyangchengji/p/6933210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值