jquery插件'uploadify' - 从上传脚本返回响应的方法?

时间:2010-10-31 22:26:45

标签: jquery jquery-plugins 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'
    });
});

AFAIK我在“add_list.php”文件中所做的就是通过将文件移动到最终目录来完成上传过程;我不认为有什么方法可以“回复某些错误”吗?

如果我可以使用这个文件来禁止某些字符或者如果出现某种问题则返回错误会很好,但我认为不存在?

我想我可以删除任何不好的字符,但知道我是否能以某种方式返回响应会很有用吗?

4 个答案:

答案 0 :(得分: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了解详情

希望有所帮助

答案 1 :(得分: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);
      }
    });
});

答案 2 :(得分: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);
      }
    });
});

答案 3 :(得分:0)

对任何可能在将来遇到这种情况的人。我花了一点时间弄清楚如何从服务器传回我自己的数据。

本文编写的当前版本的uploadify是3.2,您可能正在寻找onUploadSuccess事件: http://www.uploadify.com/documentation/uploadify/onuploadsuccess/

这将允许您从服务器获取返回的数据。