2010-10-31 65 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/

这将允许你从服务器返回的数据。