title: Sending intermittent response from ASP.NET Web API method to jquery ajax
body: <p>I am working on a task where I need to send intermittent status response from Web API method back to the jquery ajax call and display the progress in UI.</p>
  
  <p><a href="https://i.stack.imgur.com/Y6R4w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y6R4w.png" alt="enter image description here"></a></p>
  
  <p>Initially, the jquery ajax will call the web Api method passing some parameters and the web Api method starts to perform long running actions. After each action is complete, I want to send a percentage (manually, some number) back to the calling jquery ajax method and show the progress in UI,</p>
  
  <p>What I have tried so far,</p>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;div class="row"&gt;
          &lt;div class="col-xs-12"&gt;
              &lt;div class="col-xs-3"&gt;
                  &lt;input type="file" id="FileInput" /&gt;
              &lt;/div&gt;
              &lt;div class="col-xs-1"&gt;
                  &lt;button type="button" class="btn btn-default btn-xs" id="UploadFileBtn"&gt;Upload&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p><strong>Typescript:</strong></p>
  
  <pre><code>        instance.importFile.on('change', function () {
              instance.selectedFile = this.files[0];
              // This code is only for demo ... (usage of FileAPI)
              console.log("name : " + instance.selectedFile.name);
              console.log("size : " + instance.selectedFile.size);
              console.log("type : " + instance.selectedFile.type);
              console.log("date : " + instance.selectedFile.lastModifiedDate);
          });
  
          $('#UploadFileBtn').on('click', function () {
              var formData = new FormData();
              formData.append('file', instance.selectedFile);
  
              $.when(FileUploadService.ProcessData(formData)).done(function () {
              }).fail(function () {
                  }).progress(function () {
                      console.log("progressing...");
              });
          });
  </code></pre>
  
  <p><strong>Web API:</strong></p>
  
  <pre><code>public class FileUploadController : ApiController
      {
      [HttpPost]
      public HttpResponseMessage Upload()
      {
  
          if (HttpContext.Current.Request.Files.Count &gt; 0)
          {
              var postedFile = HttpContext.Current.Request.Files[0];
  
              var fileNameParts = postedFile.FileName.Split('\\');
              var fileName = fileNameParts[fileNameParts.Length - 1];
  
              fileName = string.Format("{0}_{1}", DateTime.Now.Ticks.ToString(), fileName);
              string filePath = Path.Combine("c:\\temp", fileName);
              postedFile.SaveAs(filePath);
          }
  
          var response = new HttpResponseMessage(HttpStatusCode.OK);
          response.Content = new StringContent("UPLOADED");
  
          System.Threading.Thread.Sleep(5000);
  
          return response;
      }
  }
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Now, I have the web api method but, I am not sure how to send the intermittent responses back to UI. I am looking for simple solutions. Any suggestion / example is appreciated.</p>