开发者

perform background processing when form is submitted

开发者 https://www.devze.com 2023-02-08 19:45 出处:网络
I have an action method which runs when a user submits a form. An image URL is passed with the model. I want to be able to perform some processing on the (downloaded) image, but i don\'t want a dela

I have an action method which runs when a user submits a form.

An image URL is passed with the model.

I want to be able to perform some processing on the (downloaded) image, but i don't want a delay in t开发者_如何学编程his processing to affect the user experience.

so how can i do this processing in the background whilst happily submitting the form? e.g.:

ProcessImage(imageUrl); //do this in the background without delaying user experience  


Until you are receiving the Image you cant change the user's page .

However after you gt the image you can do the required processing in BackGroundWorker and submit the page. And let the user see new required Page.


Show a busy screen on the client that lets the user know that he's waiting for a file download. You could achieve this i.e. by downloading to a separate iframe and checking the status of the iframe (for a dynamic approach see Dynamically created iframe used to download file triggers onload with firebug but not without)


You can use jQuery to post the imageurl. Your callback will be called when it's done.

function ProcessImageUrl(imgurl) 
{ 
   $.post('<%= Url.Action("AjaxProcessImage","AjaxFunctions") %>'
      , { url: imgurl }
      , function (data) {
         if (data.success) {
           alert("I'm done processing the image");
         }
         else {
            alert("Darn an error occurred: " + data.msg);
         }
   });
}

In a controller called AjaxFunctionsController

   public ActionResult AjaxProcessImage(string url)
   {
      try
      {
          ProcessImage(url);
      }
      catch(System.Exception ex)
      {
         return Json(
               new
               {
                  success = false,
                  msg = ex.Message
               });
       }

      return Json(
            new
            {
               success = true
            });
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消