代码之家  ›  专栏  ›  技术社区  ›  Brett

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

  •  5
  • Brett  · 技术社区  · 14 年前

    我的标题代码:

    $(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”文件中,我所能做的就是通过将文件移动到final dir来完成上传过程;我不认为有任何方法可以“返回”错误之类的东西,对吧?

    如果我也可以使用这个文件来禁止某些字符,或者在出现某种问题时返回一个错误,这将是很好的,但我不认为有问题吗?

    我想我可以去掉任何不好的字符,但如果我能以某种方式返回一个响应,这会很有用吗?

    4 回复  |  直到 12 年前
        1
  •  7
  •   Lorenzo OnoSendai    14 年前

    可以向上载脚本中添加一些事件处理程序,以检查是否有完整的操作和错误

    $('#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 更多细节

    希望有帮助

        2
  •  1
  •   RonnieSan    14 年前

    $(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);
          }
        });
    });
    
        3
  •  0
  •   portapipe    14 年前

    如果需要文件名,则“必须”使用(正确的方法)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);
          }
        });
    });
    
        4
  •  0
  •   Curt Mullin    12 年前

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

    http://www.uploadify.com/documentation/uploadify/onuploadsuccess/

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

    推荐文章