【问题标题】:Stop execution of mvc controller when button is pressed按下按钮时停止执行 mvc 控制器
【发布时间】:2015-01-14 05:58:15
【问题描述】:

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

 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" });
        }
    }

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

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

我该如何解决这个问题?

【问题讨论】:

  • 文件是如何上传的?在一个线程中?
  • 我编辑代码以显示线程中的所有命令

标签: javascript asp.net-mvc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多