I am using jQuery.Form to upload files on server. Here is an sample I am using http://aspzone.com/tech/jquery-file-upload-in-asp-net-mvc-without-using-flash/
I wan to to validate file on server for NULL and for extension,
public FileUploadJsonResult UploadSubscriptions(HttpPostedFileBase file)
{
if (file == null)
return new FileUploadJsonResult { Data = new { message = string.Format(" Error uploading file. Choose the file") } };
if(Path.GetExtension(file.FileName).ToLower() != "csv")
return new FileUploadJsonResult { Data = new { message = string.Format("{0} Error uploading file. Invalid extension.", System.IO.Path.GetFileName(file.FileName)) } };
//Doing the thing here...
return new FileUploadJsonResult { Data = new { message = string.Format("{0} uploaded successfully.", System.IO.Path.GetFileName(file.FileName)) } };
}
but in all of the cases if i will return FileUploadJsonResult on client it will be successful result, function success
will be executed:
success: function (result) {
//debugger;
$("#ajaxUploadForm").unblock();
$("#ajaxUploadForm").resetForm();
$.growlUI(null, result.message);
},
error: function (xhr, textStatus, errorThrown) {
$("#ajaxUploadForm").unblock();
$("#ajaxUploadForm").resetForm();
$.growlUI(null, 'Error uploading file. Try again later');
$('div.growlUI').css("background", "url(attention48.png) no-repeat 10px 10px");
}
In such case i may edit the function 'success' to put conditions to check result.message and then display growlUI with error, but i would rather generate error on server to execute client function error
How can i do that?
UPDATE:
here is my 'success' funciton modification to handle errors:
success: function (result) {
//debugger;
$("#ajaxUploadForm").unblock();
$("#ajaxUploadForm").resetForm();
$.growlUI(null, result.message);
if (result.message.indexOf("Error") != -1)
$('div.growlUI').css("background", "url(attention48.png) no-repeat 10px 10px");
},