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/'
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.
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?
Member
143 Points
308 Posts
Delete all files from a folder via HTTP call to WebAPI
Oct 05, 2017 07:48 PM|johnzee|LINK
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
All-Star
48570 Points
18086 Posts
Re: Delete all files from a folder via HTTP call to WebAPI
Oct 05, 2017 08:36 PM|PatriceSc|LINK
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?
Member
143 Points
308 Posts
Re: Delete all files from a folder via HTTP call to WebAPI
Oct 05, 2017 08:36 PM|johnzee|LINK
Never mind it turned out to be a refreshing issue, it works fine with GET method.