head>
<title>Index</title>
<style type="text/css">
#statusBorder
{
position:relative;
height:5px;
width:100px;
border:solid 1px gray;
display:none;
}
#statusFill{
position:absolute;
top:;
left:;
width:0px;
background-color:Blue;
height:5px;
}
</style>
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var uniqueId = " @Guid.NewGuid().ToString()"; $(document).ready(function (event) {
$('#startProcess').click(function () {
$.post("Home/StartLongRunningProcess",{ id: uniqueId }, function (data) {
if (data = "null") {
alert("文件为空!");
}
else {
$('#statusBorder').show();
getStatus();
}
}, "multipart/form-data");
event.preventDefault;
});
}); function getStatus() {
var url = 'Home/GetCurrentProgress/' + uniqueId;
$.get(url, function (data) {
if (data != "") {
$('#status').html(data);
$('#statusFill').width(data);
window.setTimeout("getStatus()", );
}
else {
$('#status').html("Done");
$('#statusBorder').hide();
alert("文件保存成功");
};
});
}
</script>
</head>
<body>
<div>
<div id="status">
</div>
<h2>@Html.Encode(ViewData["Message"]) </h2>
<div>
<input id="File1" type="file" name="file" />
<input id="startProcess" type="submit" value="提交" />
</div>
<br />
<div id="statusBorder">
<div id="statusFill">
</div>
</div>
</div>
</body>
</html>

Action代码

    public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
ViewData["Message"] = "Ajax Progress Bar Example";
return View();
} delegate string ProcessTask(string id);
MyLongRunningClass longRunningClass = new MyLongRunningClass(); public ContentResult StartLongRunningProcess(string id)
{
var file=Request.Files["file"];
if (file != null)
{
file.SaveAs(Server.MapPath("~/Content/" + file.FileName));
longRunningClass.Add(id);
ProcessTask processTask = new ProcessTask(longRunningClass.ProcessLongRunningAction);
processTask.BeginInvoke(id, new AsyncCallback(EndLongRunningProcess), processTask);
return Content("ok");
}
else
{
return Content("null");
}
} public void EndLongRunningProcess(IAsyncResult result)
{
ProcessTask processTask = (ProcessTask)result.AsyncState;
string id = processTask.EndInvoke(result);
longRunningClass.Remove(id);
} public ContentResult GetCurrentProgress(string id)
{
this.ControllerContext.HttpContext.Response.AddHeader("cache-control", "no-cache");
var currentProgress = longRunningClass.GetStatus(id).ToString();
return Content(currentProgress);
}
public ActionResult Upload(HttpPostedFileBase[] fileToUpload)
{
ViewBag.Message = "File(s) uploaded successfully";
return RedirectToAction("Index");
}

cs代码

public class MyLongRunningClass
{
private static object syncRoot = new object(); private static IDictionary<string, int> ProcessStatus { get; set; } public MyLongRunningClass()
{
if (ProcessStatus == null)
{
ProcessStatus = new Dictionary<string, int>();
}
} public string ProcessLongRunningAction(string id)
{
for (int i = ; i <= ; i++)
{
Thread.Sleep();
lock (syncRoot)
{
ProcessStatus[id] = i;
}
}
return id;
} public void Add(string id)
{
lock (syncRoot)
{
ProcessStatus.Add(id,);
}
} public void Remove(string id)
{
lock (syncRoot)
{
ProcessStatus.Remove(id);
}
} public int GetStatus(string id)
{
lock (syncRoot)
{
if (ProcessStatus.Keys.Count(x => x == id) == )
{
return ProcessStatus[id];
}
else
{
return ;
}
}
}
}

【ASP.NET MVC】HTML5+MVC上传文件显示进度的更多相关文章

  1. ajax上传文件显示进度

    下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...

  2. jQuery上传文件显示进度条

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script sr ...

  3. HTML5上传文件显示进度

    下面我们使用Html 5的新特性file api实现上传文件,并显示上传文件进度百分比.意图是这样的,当选择文件时,显示当前文件信息.这里我们是结合Asp.net MVC做为服务端,您也可以是其它的服 ...

  4. axios+Vue上传文件显示进度

    一,前言 最近在用Vue,然后上传文件时需要显示进度,于是网上搜了一下,经过自己实测终于也弄明白了 二,效果 三,代码 HTML代码 <div id="app"> &l ...

  5. Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)

    Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...

  6. asp.net中FileUpload得到上传文件的完整路径

    asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...

  7. asp dotnet core 支持客户端上传文件

    本文告诉大家如何在 asp dotnet core 支持客户端上传文件 新建一个 asp dotnet core 程序,创建一个新的类,用于给客户端上传文件的信息 public class Kanaj ...

  8. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  9. ASP.NET MVC 4 批量上传文件

    上传文件的经典写法: <form id="uploadform" action="/Home/UploadFile" method="post& ...

随机推荐

  1. Jenkins自动化构建系列:01敏捷开发、自动化构建与持续集成

    <SVN与TortoiseSVN实战系列>已写完,今天新开一个<Jenkins自动化构建系列>,上周听了Bob Jiang老师的Agile1001公开课,一直想写个总结,这篇关 ...

  2. phantomjs 无法打开https网站

    最近一直在用phantomjs 自动登陆并爬取一些数据,突然发现爬取https类型的网站的时候无法正常操作了 困扰了两天的问题在经过google和stackoverflow的一番搜索后发现原来Phan ...

  3. ZooKeeper文档(二)

    ZooKeeper:因为协调的分布式系统是一个动物园 ZooKeeper对分布式应用来说是一个高性能的协调服务.它暴露通常的服务-比如命名,配置管理,同步,和组服务-用一种简单的接口,所以你不用从头开 ...

  4. 1-shell学习(bash)

    1.为什么需要学习shell: (1)通用性,基本上所有的linux机器都会支持 (2)文字传输操作更快 (3)以后的系统管理需要使用 2.知识点: (1)变量相关:

  5. 51Nod 1090 3个数之和

    Input示例 7 -3 -2 -1 0 1 2 3 Output示例 -3 0 3 -3 1 2 -2 -1 3 -2 0 2 -1 0 1 #include "bits/stdc++.h ...

  6. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...

  7. [洛谷P2023] [AHOI2009]维护序列

    洛谷题目链接:[AHOI2009]维护序列 题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列 ...

  8. C11简洁之道:初始化改进

    1.  C++98/03初始化 我们先来总结一下C++98/03的各种不同的初始化情况: //普通数组 ] = {, , }; //POD(plain old data) struct A { int ...

  9. 【Codeforces711E】ZS and The Birthday Paradox [数论]

    ZS and The Birthday Paradox Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample ...

  10. centos6.5下安装svn并且实现多项目管理配置方案

    #安装SVN服务器 yum install subversion #在home下创建svn根目录 mkdir /home/svn #在 /home/svn下创建pro1 , pro2, pro3 三个 ...