私の 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: