【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& ...
随机推荐
- 数据结构&字符串:字典树
前缀树里面可以存一堆字符串,也可以说是一堆单词,存完之后我们可以轻松判断一个指定的字符串是否出现过 下面我来详细解释一下实现细节 *+; //单词个数*每一个单词的字符数 ; struct Trie ...
- HDFS error
错误信息描述: HDFS error: could only be replicated to 0 nodes, instead of 1;以及由此衍生出来的种种奇葩问题(具体的错误信息见后面),下面 ...
- Linux 使用代理使网速变快
$ export http_proxy="http://USER:PASSWORD@PROXY_SERVER:PORT" $ export https_proxy="ht ...
- Quick-Cocos2dx-Community_3.6.3_Release 中 tolua++ 使用方法
参考文章1 http://www.aichengxu.com/view/45851 参考文章2 http://blog.csdn.net/pawleft/article/details/5212744 ...
- 洛谷 P3375 【模板】KMP字符串匹配
我这段时间因为字符串太差而被关了起来了(昨晚打cf不会处理字符串现场找大佬模板瞎搞,差点就凉了),所以决定好好补一下字符串的知识QAQ,暂时先学习kmp算法吧~ 题目链接:https://www.lu ...
- python模块subprocess学习
当我们想要调用系统命令,可以使用os,commands还有subprocess模块整理如下: os模块: 1. os.system 输出命令结果到屏幕.返回命令执行状态. >>> o ...
- 第一章:读取文件一行IO::File
#!c:\\perl\\bin\\perl.exe use IO::File; #读取一行 my $fd = IO::File->new('perl.txt'); my $one_line = ...
- go环境变量及build文件
package main /* windows go环境设置: # 参考:https://blog.csdn.net/quicmous/article/details/80360126 GOROOT ...
- C函数前向声明省略参数
这样的不带参数的函数声明,在c中是合法的,表示任意参数:当然我们自己写代码最好不要这样写了,但是读老代码还是会遇到: #include <stdio.h> void fun(); int ...
- 64_c1
CBFlib-0.9.5.15-3.fc26.i686.rpm 05-Feb-2017 21:55 427710 CBFlib-0.9.5.15-3.fc26.x86_64.rpm 05-Feb-20 ...