0

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");
            },
Joper
  • 8,019
  • 21
  • 53
  • 80

1 Answers1

0

Success and error refer to the call you make to the server. An error is if the addres is unreachable or there are connection problems, it's not related to the logic of your application. You should handle errors in the flow of your application (like a not allowed extension) with conditional messages and behaviours in the success function, because there were no problem in the call itself. At least that's what i think.

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192