在我的 MVC 应用程序中,我使用 uploadify 进行文件上传。控制器动作是:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData, FormCollection forms)
{
try
{
if (fileData.ContentLength > 0)
{
var statusCode = Helper.UploadList();
if (statusCode.Equals(System.Net.HttpStatusCode.Created))
return Json(new { success = true });
}
}
return Json(new { success = false });
}
catch (Exception ex)
{
}
我根据 StatusCode 设置了 success= true/false 并将其传递给 uploadify 的 onComplete 事件(在 .js 文件中)并显示一些有意义的警报。
如果 Helper.UploadList() 抛出如下异常:
throw new ArgumentException("Doesn't exist");
如何在控制器操作中捕获该异常并最终将其传递给 .js 文件,以便向用户显示错误?
编辑: 如果我设置 return Json(new { success = false }); 在 catch 块中,该值未达到 onComplete。
'onComplete': function (event, queueID, fileObj, response, data) {
if (response == '{"success":true}') {
alert("file uploaded successfully.");
}
else if (response == '{"success":false}') {
alert('file failed to upload. Please try again!');
}
return false;
},
I see this on the UI: