简体   繁体   English

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

[英]jquery plugin 'uploadify' - Way to return a response from upload script?

My header code: 我的标题代码:

$(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 all I can do in the "add_list.php" file is finish the upload process off by moving the file to the final dir; 我可以在“add_list.php”文件中执行的AFAIK是通过将文件移动到最终目录来完成上传过程; I don't think there is any way I can 'return something' like an error right? 我不认为有什么办法可以像错误一样'回复'吗?

It would be good if I could use this file also to disallow certain characters or return an error if there was some kind of problem, but I don't think there is? 如果我还可以使用这个文件来禁止某些字符或者如果出现某种问题则返回错误,那会很好,但我不认为有?

I guess I could just strip out any bad characters, but would be useful to know if I can return a response somehow? 我想我可以删除任何不好的字符,但知道我是否能以某种方式返回响应会很有用吗?

You can add some event handlers to your upload script to check for complete action and for error 您可以向上传脚本添加一些事件处理程序,以检查完整操作和错误

$('#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.
    }

});

So, as the onComplete function will have the response sent back from the server side script, you can return a response to the client and then parse the response inside the event handler. 因此,由于onComplete函数将从服务器端脚本发回响应,因此您可以向客户端返回响应,然后在事件处理程序内解析响应。

Check the Uploadify documentation for more details 有关更多详细信息, 查看Uploadify文档

Hope it helps 希望能帮助到你

Anything that is echoed in your add_list.php file is sent to the onComplete function as response. 在add_list.php文件中回显的任何内容都将作为响应发送到onComplete函数。 So you could do the following: 所以你可以做到以下几点:

$(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);
      }
    });
});

To anybody who may run into this in the future. 任何可能在将来遇到这种情况的人。 It took me a little bit to figure out how to pass my own data back from the server. 我花了一点时间来弄清楚如何从服务器传回我自己的数据。

The current version of uploadify at this writing is 3.2 and you are probably looking for the onUploadSuccess event: http://www.uploadify.com/documentation/uploadify/onuploadsuccess/ 本文中当前版本的uploadify是3.2,您可能正在寻找onUploadSuccess事件: http ://www.uploadify.com/documentation/uploadify/onuploadsuccess/

That will allow you to get returned data from the server. 这将允许您从服务器获取返回的数据。

If you want the name of the file, you "must" use (the correct method) fileObj.name: 如果你想要文件的名称,你必须“使用(正确的方法)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);
      }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM