按下按钮时停止执行mvc控制器

时间:2014-11-17 10:36:58

标签: javascript asp.net-mvc

我有文件上传项目,当用户按下(添加文件)并选择所需文件时,执行以下功能以将此文件保存在服务器上

 public ActionResult SaveUploadedFile()
    {

        string fName = DateTime.Now.ToString("yyyyMMddHHmmssffffff");

        bool isSavedSuccessfully = true;

        foreach (string fileName in Request.Files)
        {

            HttpPostedFileBase file = Request.Files[fileName];

            if (file != null && file.ContentLength > 0)
            {

                var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\PostImages\\DeviceImages\\", Server.MapPath(@"\")));

                string pathString = originalDirectory.ToString();



                bool isExists = System.IO.Directory.Exists(pathString);

                if (!isExists)
                    System.IO.Directory.CreateDirectory(pathString);



                string type = file.FileName.Substring(file.FileName.IndexOf("."), file.FileName.Length - file.FileName.IndexOf("."));

                fName = fName + type;
                var path = string.Format("{0}\\{1}", pathString, fName);
                file.SaveAs(path);

            }

        }

        if (isSavedSuccessfully)
        {
            UploadImagesNames.Add(fName);
            return Json(new { Message = fName });
        }
        else
        {
            return Json(new { Message = "Error in saving file" });
        }
    }

上传文件时,会出现一个进度条和删除按钮,如图所示

enter image description here

当文件完全上传时,我可以按(删除文件)删除文件,问题是当我没有完全上传文件时按下(删除文件)按钮,换句话说,我需要按下(删除文件)按钮时停止执行SaveUploadedFile()函数的方法。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您是否研究过异步操作?还有一个类似的问题here以及ermagana提供的示例项目:https://github.com/ermagana/AsyncCancelExample

相关问题