微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BootStrap Progressbar 实现大文件上传的进度条的实例代码

1.首先实现大文件上传

,如果是几兆或者几十兆的文件就用基本的上传方式就可以了,但是如果是大文件上传的话最好是用分片上传的方式。我这里主要是使用在客户端进行分片读取到服务器段,然后保存,到了服务器段读取完了之后将分片数据进行组合。

2.前端代码如下:

rush:js;"> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadTest2.aspx.cs" Inherits="Html5UploadTest.UploadTest2" %> <Meta charset="utf-8"> HTML5大<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>分片<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>示例 <%--

3. 后台一般处理程序如下:

rush:js;"> using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace Html5UploadTest { /// /// Summary description for Upload /// public class Upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; try { //从Request中取参数,注意上传文件在Requst.Files中 string name = context.Request["name"]; int total = Convert.ToInt32(context.Request["total"]); int index = Convert.ToInt32(context.Request["index"]); var data = context.Request.Files["data"]; //保存一个分片到磁盘上 string dir = context.Request.MapPath("~/temp"); string file = Path.Combine(dir,name + "_" + index); data.SaveAs(file); //如果已经是最后一个分片,组合 //当然你也可以用其它方法比如接收每个分片时直接写到最终文件的相应位置上,但要控制好并发防止文件锁冲突 if (index == total) { file = Path.Combine(dir,name); //byte[] bytes = null; using (FileStream fs = new FileStream(file,FileMode.OpenorCreate)) { for (int i = 1; i <= total; ++i) { string part = Path.Combine(dir,name + "_" + i); //bytes = System.IO.File.ReadAllBytes(part); //fs.Write(bytes,bytes.Length); //bytes = null; System.IO.File.Delete(part); fs.Close(); } } } } catch (Exception) { throw; } //返回是否成功,此处做了简化处理 //return Json(new { Error = 0 }); } public bool IsReusable { get { return false; } } } }

4.当然了后台还需要一些异常处理或者断电续传的工作要做,待续。。。

以上所述是小编给大家介绍的BootStrap Progressbar 实现大文件上传的进度条的实例代码。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐