uploadify onError未调用

时间:2012-04-17 21:18:18

标签: jquery asp.net-mvc uploadify

我们如何将自定义错误消息传递给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);
                },

1 个答案:

答案 0 :(得分:1)

修改

This post可以帮助您解决在uploadify中使用JSON的问题。您需要包含this file或等效项才能使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;
                 },