I have a JS function:
$(document).on('click', '#submitForm', function (e) {
var fileName = $('#fileName').val();
$.ajax({
type: 'POST',
url: '/Calculation/FileExist',
data: { 'fileName': fileName },
dataType: 'bool',
success: function (result) {
if (result.returnvalue) {
e.preventDefault();
alert(result.returnvalue);
alert("The filename already exists. Please choose another one");
}
else {
alert("The file doesn't exist");
}
}
});
});
My action:
public ActionResult FileExist(string fileName)
{
bool result = true;
string path = Server.MapPath(TempPath) + fileName + ".xlsx"; //Path for the file
string[] Files = Directory.GetFiles(Server.MapPath(TempPath));
for (int i = 0; i < Files.Length; i++)
{
if (path == Files[i])
{
//The filename already exists
result = false;
}
}
return Json(new { returnvalue = result });
}
What am I doing wrong here? I'm trying to get the bool value from FileExist method, and if it's true stop the form from submitting (e.preventDefault)