在我的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虚假并将它传递给uploadify的onComplete时间(在.js文件),并显示一些有意义的警报。
如果Helper.UploadList()抛出类似的异常:
throw new ArgumentException("Doesn't exist");
我怎样才能捕捉到控制器的行动,异常,并最终把它传递给.js文件,这样我可以显示一个错误的用户?
编辑: 如果我设置返回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:
它看起来像你已经有一个'try/catch'块 - 是问什么特定的JSON响应将uploadify接受显示som ething? – Tejs 2012-04-17 19:33:14
如果我在try/catch中设置成功= false。我如何将它返回给.js? – GoldenUser 2012-04-17 19:36:49
'return Json(new {success = false});'?您仍然可以从catch块返回JSON。 – Tejs 2012-04-17 19:37:16