jQuery表单文件上传检查文件在服务器上并生成错误以传递给客户端

我正在使用jQuery.Form上传服务器上的文件。 以下是我正在使用的示例http://aspzone.com/tech/jquery-file-upload-in-asp-net-mvc-without-using-flash/

我想validation服务器上的文件是否为NULL和扩展,

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)) } }; } 

但在所有情况下,如果我将在客户端返回FileUploadJsonResult,它将成功结果,将执行函数success

 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"); } 

在这种情况下,我可以编辑函数’success’来放置条件来检查result.message然后显示growlUI有错误,但我宁愿在服务器上生成错误来执行客户端函数error我该怎么做?

更新:

这是我处理错误的’成功’function修改:

 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"); }, 

Successerror是指您对服务器的调用。 如果addres无法访问或存在连接问题,则会出错,它与应用程序的逻辑无关。 您应该在成功函数中使用条件消息和行为来处理应用程序流中的错误(如不允许的扩展),因为调用本身没有问题。 至少这就是我的想法。