-1

我们如何将自定义错误消息传递给uploadify?

如果在控制器动作上,有一个异常(由 try/catch 捕获)——我们如何将它传递给 uploadify 脚本?永远不会调用 onError 事件?

    [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)
    {  
    return Json(new { success = false });       
    }   
}

    '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!');                   
                    }
                    else {
                        $("#file_uploadDomain").uploadifyCancel(queueID);
                    }
                    return false;
                },

                'onError': function(event, ID, fileObj, errorObj) {
                    alert(errorObj.type + ' Error: ' + errorObj.info);
                },
4

1 回答 1

1

编辑

这篇文章应该可以帮助您解决使用 JSON 和 uploadify 的问题。您需要包含此文件或等效文件才能使 JSON.parse 正常工作。

像这样的东西应该可以工作 - 使用 JSON 来发挥你的优势

 [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, message = "No file was specified." });        
    }
    catch (Exception ex)
    {  
    return Json(new { success = false, message = ex.ToString() });       
    }   
}

    'onComplete': function (event, queueID, fileObj, response, data) {
                    var json = JSON.parse(response);
                    if (json.success) {
                        alert("File uploaded successfully.");
                    }
                    else if (!json.success) {
                        alert(json.message);                   
                    }


  //not sure what else you could have here for the value of success
//, thus a redundant else statement, but I will leave it in.  
                    else {
                       $("#file_uploadDomain").uploadifyCancel(queueID);
                    }
                    return false;
                 },
于 2012-04-17T22:39:25.877 回答