获取Uploadify错误:uploadifySettings不是一个功能

时间:2013-03-23 21:45:04

标签: javascript error-handling uploadify

我正在努力让用户选择他们想要使用Uploadify将文件上传到哪个文件夹。我有一堆javascript代码,但除最后一点外它都有效。如果你愿意,我可以展示一切,但我坚信这不是问题。

这是我的代码:

$("#file_upload").uploadify({
        'buttonText'    : 'Upload New Files',
        'height'        : 30,
        'swf'           : 'uploadify/uploadify.swf',
        'uploader'      : 'uploadify/uploadify.php',
        'width'         : 120,
        'folder'        : $('#folder').val(),
        'auto'          : true,
        'multi'         : true,
        'onUploadComplete' : function(file) {
            location.reload();
        }
    });

    $("#folder").change(function () {
        var path = "/" + $(this).val();
        $('#file_upload').uploadifySettings("scriptData", {'folder': path });
        alert(path);
    });

此代码包含在document.ready()。

此代码的作用是

  1. 初始化uploadify功能
  2. 当用户更改#folder的值(即下拉列表)时,uploadify函数中的值“folder”会发生变化。
  3. 当我点击选择框时,没有任何反应。如果我注释掉这一行:

    $('#file_upload').uploadifySettings("scriptData", {'folder': path });
    

    我得到警报,如果我离开它就行不了。

    我咨询了Firebug,发现我收到了以下错误:

    TypeError: $(...).uploadifySettings Not A Function
    

    从逻辑上讲,我会说这意味着我没有将uploadify链接到页面上。但是,如果我上传文件,它会上传,所以它就在那里并且有效。

    这是我的HTML / PHP:

    <h3>Your Uploaded Files</h3>
        <input type="file" name="file_upload" id="file_upload">
        <span style="margin: 5px 10px 0 0; float: left;">to</span>
        <select id="folder">
            <?php
            echo '<option value="'.$directory.'">Downloads</option>';
            $friendlyDir;
            foreach(glob('downloads/*', GLOB_ONLYDIR) as $dir) {
              $friendlyDir = str_replace("downloads/","",$dir);
              echo '<option value="'.$dir.'">'.ucfirst(strtolower($friendlyDir)).'</option>';
            }
            ?>
        </select>
        <div id="file_viewer"></div>
    

    HTML / PHP工作正常,但我想我会把它包括在内,这样你就可以看到触发器等等。

    如果有人可以调查一下,我会感激不尽。我没有进行跨浏览器测试,但我使用的浏览器是Firefox 19.0.2。

    谢谢。

1 个答案:

答案 0 :(得分:1)

我最后这样做是为了使用uploadify函数进行归档:

var path;
    $("#file_upload").uploadify({
        'buttonText'    : 'Upload New Files',
        'height'        : 30,
        'swf'           : 'uploadify/uploadify.swf',
        'uploader'      : 'uploadify/uploadify.php',
        'width'         : 120,
        'folder'        : $('#folder').val(),
        'auto'          : true,
        'multi'         : true,
        'folder'        : path,
        'onUploadStart' : function(file) {
           $('#file_upload').uploadify("settings", 'formData', {'folder' : path});
        },
        'onUploadSuccess' : function(file, data, response) {
            //alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
            location.reload();
        }
    });

    $("#folder").change(function () {
        path = "/" + $(this).val();
    });

在uploadify.php上我改为:

//$targetFolder = '/file/downloads'; // Relative to the root
$targetFolder = "/file".$_POST['folder'];

if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','pdf','doc','docx'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo $targetFolder;
} else {
    echo 'Invalid file type.';
}
}

现在它有效:D