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. ActionMQ5.8.0 JMS实例 手把手详细图解

    出自:http://blog.csdn.net/tongjie008/article/details/40687087 ActionMQ 是开源的JMS实现 1.下载ActiveMQ 去官方网站下载: ...

  2. Squid Linux 代理服务器

    简介: Squid 是 Linux/Unix 平台下最为流行的高性能免费应用层代理服务器,它具有权限管理灵活.性能高和效率快的特点. 代理服务器可以提供文件缓存.复制和地址过滤等服务,充分利用有限的出 ...

  3. js里面的三种注释方法

    javascript(js)语言里面的注释方法有三种. 第一种是多行注释"/**/",一般js文件开头,介绍作者,函数等信息. /* *author:xxx *day:2008-0 ...

  4. cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题

    http://blog.csdn.net/eguid_1/article/details/73611781

  5. Elasticsearch全文检索,高亮关键字

    问题 用如下这样的term方式,可以高亮 .setQuery(QueryBuilders.termQuery("PARAM_NAME", "a")) { &qu ...

  6. 使用net.sf.fjep.fatjar插件将第三方JAR包打包进自已的JAR包中

    一般单个工程,在没有应用别人的jar包时导出为jar很简单,只要设置一个Main-Class就行了,也就是选择程序入口(main所在类).但是涉及到了数据库或需要用到第三方的JAR,就需要用到相应的数 ...

  7. Scala基础:定义变量和逻辑判断语句以及方法和函数

    定义变量和逻辑判断语句 package com.zy.scala import scala.collection.immutable object ScalaDemo { def main(args: ...

  8. Spark internal - 多样化的运行模式 (下)

    Spark的各种运行模式虽然启动方式,运行位置,调度手段有所不同,但它们所要完成的任务基本都是一致的,就是在合适的位置安全可靠的根据用户的配置和Job的需要管理和运行Task,这里粗略的列举一下在运行 ...

  9. 使用CocoaPods卡在了"pod setup"界面的解决办法

      http://blog.csdn.net/samoy/article/details/51956799   有时候,我们在执行pod install或pod search命令时,会在终端偶现卡在’ ...

  10. eclipse+hbase开发环境部署

    一.前言 1. 前提 因为hbase的运行模式是伪分布式,需要用到hdfs,所以在此之前,我已经完成了hadoop-eclipse的开发环境搭建,详细看另一篇文章:hadoop开发环境部署——通过ec ...