2012-04-17 143 views
-1

我們如何將自定義錯誤消息傳遞給uploadify?uploadify onError not called

如果在控制器操作上有一個異常(由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

編輯

This post應該幫助你解決使用JSON與uploadify。您需要包含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; 
       }, 
+0

那會已經容易得多......但response.success在的onComplete不確定的。 – GoldenUser 2012-04-17 22:51:39

+0

沒關係,你是對的... – Tommy 2012-04-17 22:55:10

+0

data.success和data.message都是undefined – GoldenUser 2012-04-17 22:56:04