2010-10-31 64 views
5

我的頭代碼:jQuery插件'uploadify' - 從上傳腳本返回響應的方式?

$(document).ready(function() { 
    $('#sampleFile').uploadify({ 
     'uploader': 'include/uploadify/uploadify.swf', 
     'script': 'add_list.php', 
     'scriptData': {'mode': 'upload'}, 
     'fileDataName': 'sampleFile', 
     'folder': '/work/avais/bizlists/lists', 
     'cancelImg': 'include/uploadify/cancel.png', 
     'queueID': 'sampleQueue' 
    }); 
}); 

據我所知,我只能在「add_list.php」文件做的是通過移動文件到最終完成目錄上傳過程中脫落;我不認爲我有什麼辦法可以像錯誤一樣「回報」什麼嗎?

如果我可以使用這個文件來禁止某些字符,或者在出現某種問題時返回一個錯誤,但是我不認爲有這種情況會很好嗎?

我想我可以刪除任何不好的字符,但會知道我是否可以以某種方式返回響應?

+0

[Uploadify:顯示來自HTTP響應的錯誤消息]可能的副本(http://stackoverflow.com/questions/1877644/uploadify-show-error-message-from-http-response) – 2010-10-31 22:29:08

回答

7

您可以將某些事件處理程序添加到您的上傳腳本來檢查完整的行動和錯誤

$('#sampleFile').uploadify({ 
     'uploader': 'include/uploadify/uploadify.swf', 
     'script': 'add_list.php', 
     'scriptData': {'mode': 'upload'}, 
     'fileDataName': 'sampleFile', 
     'folder': '/work/avais/bizlists/lists', 
     'cancelImg': 'include/uploadify/cancel.png', 
     'queueID': 'sampleQueue' 

    onComplete: function (event, queueID, fileObj, response, data) { 
     // A function that triggers when a file upload has completed. The default 
     // function removes the file queue item from the upload queue. The 
     // default function will not trigger if the value of your custom 
     // function returns false. 
     // Parameters 
     // event: The event object. 
     // queueID: The unique identifier of the file that was completed. 
     // fileObj: An object containing details about the file that was selected. 
     // response: The data sent back from the server. 
     // data: Details about the file queue. 
    }, 

    onError: function (event, queueID, fileObj, errorObj) { 
     // A function that triggers when an error occurs during the upload process. 
     // The default event handler attaches an error message to the queue item 
     // returning the error and changes it's queue item container to red. 
     // Parameters 
     // event: The event object. 
     // queueID: The unique identifier of the file that was errored. 
     // fileObj: An object containing details about the file that was selected. 
     // errorObj: An object containing details about the error returned. 
    } 

}); 

所以,和onComplete功能將有來自服務器端腳本發回的響應,可以退貨對客戶端的響應,然後解析事件處理程序中的響應。

檢查Uploadify documentation更多細節

希望它可以幫助

+0

謝謝 - 我知道這些處理程序雖然不知道onComplete函數將結果返回給服務器端腳本。 :) – Brett 2010-11-01 15:55:41

1

凡是在您add_list.php文件呼應發送到的onComplete功能響應。所以,你可以做到以下幾點:

$(document).ready(function() { 
$('#sampleFile').uploadify({ 
    'uploader': 'include/uploadify/uploadify.swf', 
    'script': 'add_list.php', 
    'scriptData': {'mode': 'upload'}, 
    'fileDataName': 'sampleFile', 
    'folder': '/work/avais/bizlists/lists', 
    'cancelImg': 'include/uploadify/cancel.png', 
    'queueID': 'sampleQueue', 
    'onComplete' : function(event,ID,fileObj,response,data) { 
     alert(response); 
     } 
    }); 
}); 
+0

非常感謝這個例子! :) – Brett 2010-11-01 15:56:01

0

如果你想要的文件的名稱,則「必須」使用(正確的方法)fileObj.name:

$(document).ready(function() { 
$('#sampleFile').uploadify({ 
    'uploader': 'include/uploadify/uploadify.swf', 
    'script': 'add_list.php', 
    'scriptData': {'mode': 'upload'}, 
    'fileDataName': 'sampleFile', 
    'folder': '/work/avais/bizlists/lists', 
    'cancelImg': 'include/uploadify/cancel.png', 
    'queueID': 'sampleQueue', 
    'onComplete' : function(event,ID,fileObj,response,data) { 
     alert(fileObj.name); 
     } 
    }); 
}); 
0

爲了誰可能碰到任何人這在未來。我花了一點時間才弄清楚如何從服務器傳回我自己的數據。

uploadify在寫這篇文章的當前版本是3.2,你可能正在尋找onUploadSuccess事件: http://www.uploadify.com/documentation/uploadify/onuploadsuccess/

這將允許你從服務器返回的數據。