在ASP.NET MVC3应用程序中,我尝试使用Html.BeginForm助手上传文件,如下所示:
<% using (Html.BeginForm("ImportFile", "Home", new { someId = Id }, FormMethod.Post, new { enctype="multipart/form-data" } )) %>
在上传过程中,从ImportFile操作中,我需要返回状态代码(比如“成功”或“失败”),并在UI上显示适当的警告。一种方法是异步调用form Action,然后从JS中每隔2秒检查一次状态代码。我怎么才能异步调用呢?或者,有没有其他方法来实现这一点?
提前谢谢。
发布于 2014-01-30 05:54:13
请记住,HTTP协议在窥探文件上传过程时有一定的限制。假设您的ImportFile帮助器可以公开此类信息,并且可以通过RESTful服务(比如importstatus.aspx )访问这些信息,下面是如何获取其状态的方法:
var statuschecker=setInterval(){
var rq=xmlHTTPRequestObject();
rq.open('GET','importstatus.aspx',true);
rq.onreadystatechange=function(){
if (rq.readyState==4&&rq.status==200){
if (rq.responseText=='2'){
clearInterval(statuschecker); //stop the updater
statuschecker=null; //prevent memory leak
return;
}
if (rq.responseText=='1'){
//display that it's still being uploaded
}
}//xhr status
}
rq.send(null);
}, 1000); //poll every second
在您的web服务中,当进程终止时返回2。当接收到term信号时,回调函数应终止观察者间隔/定时器。
https://stackoverflow.com/questions/21448799
复制