title: Delete file using jquery ajax in asp.net c#
body: <p>I have a list of files contains pdf doc txt etc from a folder..and i listed in html format ie filename and delete button in the list(not using gridview).
  When a user click the delete button file has to delete using ajax not postback method.</p>
  
  <p>Here is my c# code for loading the files.</p>
  
  <pre><code>if (Directory.Exists(path))
         {
             string[] filePaths = Directory.GetFiles(path);
             List&lt;ListItem&gt; files = new List&lt;ListItem&gt;();
             htmlStr = "&lt;table class='tblDocList'&gt;&lt;tr class='trDocListHead'&gt;&lt;td colspan='3'&gt;Existing Documents&lt;/td&gt;&lt;/tr&gt;";
             foreach (string filePath in filePaths)
             {
                 files.Add(new ListItem(Path.GetFileName(filePath), filePath));
                 htmlStr += "&lt;tr class='trDataRow'&gt;";
                 htmlStr += "&lt;td class='tdFileName'&gt;" + Path.GetFileName(filePath) + "&lt;/td&gt;";
                 htmlStr += "&lt;td class='tdDownloadLink'&gt;&lt;a href class='downloadLink' name='" + filePath + "'&gt;Download&lt;/a&gt;&lt;/td&gt;";
                 htmlStr += "&lt;td class='tdDownloadLink'&gt;&lt;a href class='deleteLink' name='" + filePath + "'&gt;Delete&lt;/a&gt;&lt;/td&gt;";
                 htmlStr += "&lt;/tr&gt;";
             }
             htmlStr += "&lt;/table&gt;";
  
          }
  </code></pre>
  
  <p>Jquery Code</p>
  
  <pre><code> $('.deleteLink').click(function (e) {
      var fPath = this.name;
      e.preventDefault();
      $.ajax({
          type: "POST",
          url: "Controls/ucAttachDocumentation/DeleteFile",
          data: { fileName: fPath},
          contentType: "application/json",
          success: function (data) {
              alert(data);
          },
          error: function (data) {
              alert(data);
          }
      });
  });
  </code></pre>
  
  <p>In user control for deleteting code</p>
  
  <pre><code>[WebMethod]
     public static bool DeleteFile(string fileName)
     {
         try
         {
             File.Delete(fileName);
             return true;
         }
         catch (Exception e)
         {
             return false;
         }
     }
  </code></pre>
  
  <p>I am getting the full filepath in jquery and try to call ajax method and it's not working ,can anybody help me out to solve this.</p>
  
  <p>Thanks
  Alex</p>