0

I have file upload project, when the user press ( add file ) and select the desired file, the following function well be executed to save this file on the server

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

As the file is uploaded, a progress bar and delete button is appeared as shown in the image

enter image description here

When the file is uploaded completely, I can press the ( Remove File ) to remove the file, The problem is that when I press the ( Remove file ) button when the file is not completely uploaded, in other words , I need a way to stop the execution of SaveUploadedFile() function when the ( Remove file ) button is pressed.

How can I solve this problem ?

Ahmed Shamel
  • 1,982
  • 3
  • 24
  • 58

1 Answers1

0

Have you looked into Async operations? There is an similar question here as well as an example project provided by ermagana: https://github.com/ermagana/AsyncCancelExample

Community
  • 1
  • 1
Joe R Casey
  • 226
  • 2
  • 8