由网友(告诉自己、要坚强)分享简介:问题是与Ajax请求取消之后,我打电话从形式ProcessMessage的提交我有问题问题提交页面取消我的Ajax请求,所以我得到的错误..请帮我在这查看@using(Html.BeginForm(指数,家,FormMethod.Post,新{ID =formUpload,ENCTYPE =的multipart /...
问题是与Ajax请求取消 之后,我打电话从形式ProcessMessage的提交我有问题
问题提交页面取消我的Ajax请求,所以我得到的错误.. 请帮我在这
查看
@using(Html.BeginForm(指数,家,FormMethod.Post,新{ID =formUpload,ENCTYPE =的multipart / form-data的}) )
{
< DIV>
< B>上传文件< / B>
<输入类型=文件名称=文件/>
<输入类型=提交值=上传文件NAME =btnUpload的onclick =progressStatus();/>< BR />
< / DIV>
< DIV>
@ ViewBag.Message
< / DIV>
< DIV的风格=宽度:30%;保证金:0汽车;>
< DIV ID =进度风格=宽度:300像素,高度:15px的>< / DIV>
< BR />
< / DIV>
}
@ Scripts.Render(〜/包/ jQuery的)
<脚本类型=文/ JavaScript的>
传播progressStatus(){
VAR oReq =新XMLHtt prequest();
oReq.open(GET,/首页/ ProcessMessage的,真正的);
oReq.send();
的setInterval(showResult,1000);
功能showResult(){
VAR的结果=;
如果(结果!== oReq.responseText){
结果= oReq.responseText;
调试器;
$(#进度)HTML(结果);
}
}
返回false;
}
< / SCRIPT>
控制器
[HttpPost]
公众的ActionResult指数(HttpPostedFileBase文件)
{
如果(文件!= NULL)
{
VAR FNAME = Path.GetFileName(file.FileName);
VAR EXIS = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(〜/存储/上传),FNAME);
如果(System.IO.File.Exists(EXIS))
{
计算机[信息] =文件+ FNAME +已经存在;
}
其他
{
尝试
{
如果(file.ContentLength大于0)
{
VAR文件名= Path.GetFileName(file.FileName);
VAR FOLDERPATH =使用Server.Mappath(〜/存储/上传);
FNAME =文件名;
VAR路径= Path.Combine(FOLDERPATH,文件名);
VAR filebytes =新的字节[file.ContentLength]
如果(!Directory.Exists(FOLDERPATH))
Directory.CreateDirectory(FOLDERPATH);
file.SaveAs(路径);
}
计算机[信息] =文件+ FNAME +上传successully;
}
赶上(例外五)
{
计算机[信息] =文件+ FNAME +无法上传;
计算机[信息] = e.Message;
}
}
}
其他
计算机[信息] =请选择文件;
返回查看();
}
公共类ProgressiveResult:的ActionResult
{
公众覆盖无效的ExecuteReuslt(ControllerContext上下文)
{
的for(int i = 0;我小于20;我++)
{
context.HttpContext.Response.Write(i.ToString());
Thread.sleep代码(2000);
context.HttpContext.Response.Flush();
}
context.HttpContext.Response.End();
}
}
这是一个返回这个结果的操作:
公众的ActionResult ProcessMessage的()
{
返回新ProgressiveResult();
}
解决方案
您必须返回虚假点击的事件处理程序取消提交表单:
<输入类型=提交值=上传文件NAME =btnUpload的onclick =progressStatus();返回false;/>
Issue Is with Ajax Request Cancel After i call the ProcessMessage from form submit i am having issue
Issue with submitting your page is canceling my ajax request, so I am getting error.. Please help me on this
View
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "formUpload", enctype = "multipart/form-data" }))
{
<div>
<b>Upload File</b>
<input type="file" name="file" />
<input type="submit" value="Upload File" name="btnUpload" onclick="progressStatus();"/><br />
</div>
<div>
@ViewBag.Message
</div>
<div style="width: 30%; margin: 0 auto;">
<div id="progressbar" style="width: 300px; height: 15px"></div>
<br/>
</div>
}
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
function progressStatus() {
var oReq = new XMLHttpRequest();
oReq.open("get", "/Home/ProcessMessage", true);
oReq.send();
setInterval(showResult, 1000);
function showResult() {
var result = "";
if (result !== oReq.responseText) {
result = oReq.responseText;
debugger;
$("#progressbar").html(result);
}
}
return false;
}
</script>
Controller
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file != null)
{
var fname = Path.GetFileName(file.FileName);
var exis = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Storage/uploads"), fname);
if (System.IO.File.Exists(exis))
{
ViewData["Message"] = "The file " + fname + " has already exists";
}
else
{
try
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var folderPath = Server.MapPath("~/Storage/uploads");
fname = fileName;
var path = Path.Combine(folderPath, fileName);
var filebytes = new byte[file.ContentLength];
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
file.SaveAs(path);
}
ViewData["Message"] = "The file " + fname + " has uploaded successully";
}
catch (Exception e)
{
ViewData["Message"] = "The file " + fname + " Could not upload";
ViewData["Message"] = e.Message;
}
}
}
else
ViewData["Message"] = "Please choose file";
return View();
}
public class ProgressiveResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
for (int i = 0; i < 20; i++)
{
context.HttpContext.Response.Write(i.ToString());
Thread.Sleep(2000);
context.HttpContext.Response.Flush();
}
context.HttpContext.Response.End();
}
}
and this is an action that returns this result:
public ActionResult ProcessMessage()
{
return new ProgressiveResult();
}
解决方案
You have to return false in click event handler to cancel submitting the form:
<input type="submit" value="Upload File" name="btnUpload" onclick="progressStatus(); return false;"/>
相关推荐
最新文章