0

我正在研究一种让用户使用 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 函数中的值“文件夹”发生变化。

当我单击选择框时,没有任何反应。如果我注释掉这一行:

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

谢谢你。

4

1 回答 1

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

于 2013-03-23T23:26:28.793 回答