Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

how to notify ajax call multiple times during web method process?

$
0
0

how to notify ajax call multiple times during web method process? 

    [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    [System.Web.Services.WebMethod(BufferResponse = true, CacheDuration = 0, EnableSession = false)]
    public static void UploadFiles1()
    {
        myClass.Upload();
        myClass.NotifyProcess += myClass_OnNotifyProcess;
    }
    private void myClass_OnNotifyProcess(string sMsg)
    {

        var h = System.Web.HttpContext.Current; ;
        System.Collections.Generic.Dictionary<string, string> d = new System.Collections.Generic.Dictionary<string, string>();
        d.Add("aa", "xxxx--" + DateTime.Now.ToString() + "  </br>");
        string s = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(d);


        h.Response.Write(s);

    }

$.ajax({
                    type: "POST",
                    url: window.location.href + "/UploadFiles",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    //dataType: "text/plain",
                    cache: false,
                    success: function (response) {
                        alert("succ--  " + response.d);
                        console.log(response);
                        console.log(response.d);
                    },
                    error: function (response) {
                        alert(response.responseText);
                        console.log(response.d);
                    }
                })
                ;

well.. the event still haven't implemented.. so i use this method instead...

    

 [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    [System.Web.Services.WebMethod(BufferResponse = true, CacheDuration = 0, EnableSession = false)]
    public static void UploadFiles()
    {
        var h = System.Web.HttpContext.Current; ;
        if (!(h.User != null && h.User.Identity.IsAuthenticated))
            return;// "";

        //h.Response.ContentType = "text/plain";
        //string s = "xxxx--" + DateTime.Now.ToString() + "  </br>";

        System.Collections.Generic.Dictionary<string, string> d = new System.Collections.Generic.Dictionary<string, string>();
        d.Add("aa", "xxxx--" + DateTime.Now.ToString() + "  </br>");
        string s = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(d);


        h.Response.Write(s);


        System.Threading.Thread.Sleep(1500);
        h.Response.Write(s);

        System.Threading.Thread.Sleep(2500);
        h.Response.Write(s);

    }


at the moment i get all the events/Thread.Sleep messages at ones..

but i would like to show the msg each at its time.

so beside using recursive ajax call to the end of process..

some body could suggest an alternative solution?   

thank you 


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>