【ASP.NET MVC】HTML5+MVC上传文件显示进度
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上传文件显示进度的更多相关文章
- ajax上传文件显示进度
下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...
- jQuery上传文件显示进度条
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script sr ...
- HTML5上传文件显示进度
下面我们使用Html 5的新特性file api实现上传文件,并显示上传文件进度百分比.意图是这样的,当选择文件时,显示当前文件信息.这里我们是结合Asp.net MVC做为服务端,您也可以是其它的服 ...
- axios+Vue上传文件显示进度
一,前言 最近在用Vue,然后上传文件时需要显示进度,于是网上搜了一下,经过自己实测终于也弄明白了 二,效果 三,代码 HTML代码 <div id="app"> &l ...
- Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)
Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...
- asp.net中FileUpload得到上传文件的完整路径
asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...
- asp dotnet core 支持客户端上传文件
本文告诉大家如何在 asp dotnet core 支持客户端上传文件 新建一个 asp dotnet core 程序,创建一个新的类,用于给客户端上传文件的信息 public class Kanaj ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
- ASP.NET MVC 4 批量上传文件
上传文件的经典写法: <form id="uploadform" action="/Home/UploadFile" method="post& ...
随机推荐
- Android数据过滤器:Filter
类图: 通常可以将SearchView和ListView结合,实现数据的搜索和过滤. 1.监听SearchView,SearchView.setOnQueryTextListener(OnQueryT ...
- 获得WebApi用Post方法获得新增数据的信息
首先,要知道webApi的基本返回方式是HttpResponseMessage,post会在响应中返回添加的对象,以及添加对象的访问地址 如:在fiddler里测试的时候 然后,我们可以根据这一点在后 ...
- Epoll模型讲解
1.流模型 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. 不管是文件,还是套接字,还是管道,我们都可以把他们看作流. 之后我们来讨论I/O的操作, ...
- 【BZOJ】2982 combination
[算法]组合数取模——lucas定理 #include<cstdio> #include<algorithm> #include<cstring> using na ...
- CRB and Candies(组合数学+求逆元+lcm)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题目: Problem Description CRB has N different cand ...
- WordPress在nginx服务器伪静态
server { listen 80; root /var/www/xxx; server_name www.xxx.com; access_log /var/log/www/xxx.log main ...
- 获取应用版本号,版本名称,包名,AppName,图标,是否是系统应用,获取手机中所有应用,所有进程
PackageManager packageManager = getPackageManager(); PackageInfo packageInfo; = packageManager.getPa ...
- 【Python学习】程序运行完发送邮件提醒
有时候我们运行一个需要跑很长时间的程序,不管是在云主机还是本地主机上运行,我们都不可能一直守在电脑面前等.所以想到使用邮件来通知提醒. 示例代码如下 # -*- coding: utf-8 -*- # ...
- centos 引导盘
# grub.conf generated by anaconda## Note that you do not have to rerun grub after making changes to ...
- HDU 6109 数据分割 并查集,SET
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6109 题意:中文题面 解法:每次都贪心地尝试将尽量多的条件放进当前这组,遇到第一个与已有条件冲突时,就 ...