locked
Delete all files from a folder via HTTP call to WebAPI RRS feed

  • Question

  • User1793652459 posted

    Hello,

    I have this code on on my HTML page, I am trying to run a function that would just delete all files from a network folder, no parameters are involved.  When a user clicks on a Delete button on the page, it makes it to the alert box but never goes into the controller and I keep getting a message that resource can't be found.

    $scope.deleteFiles = function () {
    alert('test')
    var request = {
    method: 'GET',
    url: '/api/DeleteFiles/'

    };

    // SEND THE REQUEST.
    $http(request)
    .success(function (d) {
    alert(d);
    })
    .error(function () {
    });


    }

    I have these 2 functions in my Api controller but it never hits the delete function, however it goes into the upload function fine:

    [Route("api/UploadFiles")]
    [HttpPost]
    public string UploadFiles()
    {
    .....
    }


    [Route("api/DeleteFiles")]
    [HttpGet]
    public string DeleteFiles()
    {
    string sPath = ""; // Doesn't hit this breakpoint
    sPath = HostingEnvironment.MapPath("~/myFolder/");

    if (File.Exists(sPath))
    {

    string[] files = Directory.GetFiles(sPath, "*.*");
    foreach (string f in files)
    {
    File.Delete(f);
    }

    }


    return "File Deleted";

    }

    Not sure what method I would use for just executing a function that returns a message after deletion.  I have tried POST and GET without any luck.  I am not sending any data, just need to call the function so it deletes files.

    Thanks

    Thursday, October 5, 2017 7:48 PM

Answers

  • User1793652459 posted

    Never mind it turned out to be a refreshing issue, it works fine with GET method. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 5, 2017 8:36 PM

All replies

  • User753101303 posted

    Hi,

    And you also have a trailing / in your successful UploadFiles call ? If I remember I saw once someone having a problem because the HttpGet attribute was mistakenly taken from another namespace. If seeing nothing obvious I would start by making both calls absolutely identical including how the JavaScript code is written.

    And so you really see a 404 not found when using F12 in your browser?

    Thursday, October 5, 2017 8:36 PM
  • User1793652459 posted

    Never mind it turned out to be a refreshing issue, it works fine with GET method. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 5, 2017 8:36 PM