title: Delete image from folder using AJAX C#
body: <p>I am trying to delete an image from a folder by using AJAX and C#. I have created a handler called Delete.ashx to delete the image. I also have an AJAX function to get the image path and pass the path to the handler</p>
  
  <p>In AJAX I have a variable that gets the path which is stored in a textbox (just for now). I make an attempt to pass that path to my handler, however the problem is that the variable in the handler which I am trying to pass the data path to is always empty.</p>
  
  <p>Here is my code:</p>
  
  <p>Handler</p>
  
  <pre><code>public void ProcessRequest(HttpContext context)
  {
      string sFileName = context.Request["sFileName"]; //this variable is always empty
      if (File.Exists(context.Server.MapPath("~/images/" + sFileName)))
      {
          File.Delete(context.Server.MapPath("~/images/" + sFileName));
          context.Response.ContentType = "text/plain";
          context.Response.Write("Image deleted Successfully!");
      }
      else
      {
          context.Response.ContentType = "text/plain";
          context.Response.Write("Image Failed to Delete!");
      }
  }
  </code></pre>
  
  <p>AJAX</p>
  
  <pre><code>$(document).ready(function () {
      $("#btnDelete").click(function () {
          removeFile();
          return false;
      });
  
      function removeFile() {
          var FileName = $("#txtPath").val();
          $.ajax({
              url: 'Delete.ashx',
              type: 'POST',
              data: { 'sFileName': FileName},
              contentType: false,
              processData: false,
              success: function (result) {
                  alert(result);
              },
              error: function (xhr, ajaxOptions, thrownError) {
                  alert(xhr.status);
                  alert(xhr.responseText);
                  alert(thrownError);
              }
          });
  
      }
  
  });
  </code></pre>
  
  <p>Please advise how I can pass data from AJAX to the handler so that the variable is not empty. Thank you.</p>