using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
using System.IO;
using Newtonsoft.Json;
public class FilesController : Controller
{
// GET: Files
public ActionResult Index()
{
return View();
}
[HttpPost]
public string FileUpload()
{
//获取文件
HttpPostedFileBase file = Request.Files["file"];
//获取一次读多大
int receiveCount = Convert.ToInt32(Request.Headers["getSize"]);
//获取偏移量
int offsetCount= Convert.ToInt32(Request.Headers["offset"]);
if (file != null)
{
string getPath = Server.MapPath("~/img/");
if (!Directory.Exists(getPath))
Directory.CreateDirectory(getPath);
byte[] getBytes = new byte[receiveCount];
int count = 0;
Stream getInputStream = file.InputStream;
//获取文件大小
long size = getInputStream.Length;
using (FileStream fs = new FileStream(getPath + file.FileName, FileMode.OpenOrCreate | FileMode.Append))
{
//从偏移量开始
getInputStream.Seek(offsetCount,SeekOrigin.Current);
while ((count = getInputStream.Read(getBytes, 0, receiveCount)) != 0)
{
fs.Write(getBytes,0,count);
FileData fd = new FileData();
fd.TotalCount = size;
fd.HasReadCount = offsetCount + count;
fd.Path = "/img/" + file.FileName;
return JsonConvert.SerializeObject(fd);
}
}
}
return null;
}
}

public class FileData
{
/// <summary>
/// 文件总大小
/// </summary>
public long TotalCount { get; set; }
/// <summary>
/// 已读大小
/// </summary>
public int HasReadCount { get; set; }
/// <summary>
/// 路径
/// </summary>
public string Path { get; set; }
}
}

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Content/jquery-3.1.1.js"></script>
<script src="~/Content/jquery.form.js"></script>
<style>
#showPrecess{
background: #0026ff;
width: 0px;
height:30px;
}
</style>
</head>
<body>
<div>
<form id="form0">
<p>
<input type="file" name="file" multiple />
<img src="" id="ShowImg" width="100" height="100" />
<div id="Precess" style="width:200px;float:left;border:1px solid #ccc;" >
<div id="showPrecess">

</div>
<span style="width:200px;float:left;" id="showNumber" ></span>
</div>
<input type="button" value="上传" onclick="GetFile()" />
<input type="button" value="暂停" onclick="pause()" />
<input type="button" value="继续" onclick="goon()" />
</p>
</form>
</div>
</body>
</html>
<script>
var totalCount = 0;//总大小
var offsetCount = 0;//偏移量
var receiveState = true;//状态
$(function () {
$("input[name=file]").change(function () {
var getFile = $("input[name=file]")[0].files[0];
var getBlog = window.URL.createObjectURL(getFile);
$("#ShowImg").attr("src", getBlog);
})
})
function GetFile() {
totalCount = 0;
offsetCount = 0;
$("#showPrecess").css("width", "0%");
upload();
}
function upload() {
var getFile = $("input[name=file]")[0].files[0];
var formData = new FormData();
formData.append("file", getFile);
$.ajax({
url: "/Files/FileUpload",
data: formData,
contentType: false,
processData: false,
cache: false,
type: 'POST',
dataType:"json",
headers: {
offset: offsetCount,
getSize:200
},
success: function (d) {
console.log(d);
totalCount = d.TotalCount;
offsetCount = d.HasReadCount;
$("#showPrecess").css("width", (offsetCount / totalCount)*100+"%");
$("#showNumber").html(((offsetCount / totalCount) *100).toFixed(2)+"%");
if (totalCount - offsetCount > 0) {
if (receiveState) {
upload();
}
}
}
})
}

function pause() {
receiveState = false;
}
function goon() {
receiveState = true;
upload();
}
</script>

MVC断点续传的更多相关文章

  1. Mvc下异步断点续传大文件

    最近公司一同事咨询了一个MVC项目下上传大文件时遇到的问题,问题描述如下: MVC项目中,当上传比较大的文件时,速度非常慢,小文件基本没有影响. 原因分析: 如果是用传统的form表单去提交的话,会将 ...

  2. Asp.net mvc 大文件上传 断点续传

    Asp.net mvc 大文件上传 断点续传 进度条   概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...

  3. Asp.net mvc 大文件上传 断点续传 进度条

    概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这篇文章,此方法确实很不错,能够稳定的上传大文件,http: ...

  4. asp.net mvc大文件上传、断点续传功能。

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  5. 被误解的MVC和被神化的MVVM(转)

    转载自:http://www.infoq.com/cn/articles/rethinking-mvc-mvvm 原文作者:唐巧 被误解的 MVC MVC 的历史 MVC,全称是 Model View ...

  6. chunkupload 文件上传断点续传组件(java) - 正式发布

    chunkupload简介 chunkupload是一款基于java语言的断点续传组件,针对文件上传,非文件下载,集成方便,使用简单. chunkupload实现如下功能: ·  实现断点续传 ·  ...

  7. ASP.NET WebAPi之断点续传下载(中)

    前言 前情回顾:上一篇我们遗留了两个问题,一个是未完全实现断点续传,另外则是在响应时是返回StreamContent还是PushStreamContent呢?这一节我们重点来解决这两个问题,同时就在此 ...

  8. .net 实现上传文件分割,断点续传上传文件

    一 介绍 断点续传搜索大部分都是下载的断点续传,涉及到HTTP协议1.1的Range和Content-Range头. 来个简单的介绍 所谓断点续传,也就是要从文件已经下载的地方开始继续下载.在以前版本 ...

  9. IOS:被误解的MVC和被神化的MVVM

    MVC的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架. ...

随机推荐

  1. Linux常用命令----基本文件系统常用命令

    1.查看当前工作目录---pwd sunny@sunny-ThinkPad-T450:~$ pwd /home/sunny sunny@sunny-ThinkPad-T450:~$ cd Worksp ...

  2. C#根据url生成唯一的key

    根据url生成唯一的idkey,记录并分享: public class UrlToUniqueKey { ); , ] { { 0L, 0L }, { -4611686018427387904L, - ...

  3. out.write(b,0,len)怎么解释?

    参数 缓冲区——要写的数据 从——开始偏移量数据 len——写的字节数 返回值 这个方法不返回一个值. 异常 IOException

  4. 什么是展uv

    uvw是对应三维模型x.y.z坐标,就像模型的皮肤一样,为了便于绘制贴图,需要将UV尽量展平 展UV就是把你建好的模型展开成平面,别人就可以在那平面上做材质,皮肤等,这些东西.例如展一个正方体的UV, ...

  5. 用 GDB 调试程序

    Linux 包含了一个叫 gdb 的 GNU 调试程序. gdb 是一个用来调试 C 和 C++ 程序的强力调试器. 它使你能在程序运行时观察程序的内部结构和内存的使用情况. 以下是 gdb 所提供的 ...

  6. CloudStack Ctrix官网版本

    手动生成keystore keytool -genkey -keystore /etc/cloudstack/management/cloud.keystore -storepass "vm ...

  7. Python学习笔记_获取当前目录和上级目录

    实验目标:获取当前目录和上级目录 系统环境: 1.OS:Win10 64位 2.Pythoh 3.7 3.实验路径:C:\Work\Python\MergeExcel 代码参考: # -*- codi ...

  8. RTX Server SDK跨服务器

    反向单点登录RTX,即通过腾讯通登录到其他的系统,登录腾讯通后其他的系统不用输入用户名和密码即可登录.可能有二种情况.腾讯通和其他系统在同一台服务器上或在不同的服务器上.要调用腾讯通的登录验证接口的系 ...

  9. Tomcat内存溢出及大小调整

    一.在使用Java程序从数据库中查询大量的数据或是应用服务器(如tomcat.jboss,weblogic)加载jar包时会出现java.lang.OutOfMemoryError异常.这主要是由于应 ...

  10. Qt5.7学习

    一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...