2013-03-23 122 views
0

我正在爲用戶選擇要將文件上傳到Uploadify的文件夾的方式。我有一堆javascript代碼,但它除了最後一點之外都可以工作。如果你願意,我可以展示一切,但我堅信這不是問題。獲取Uploadify錯誤:uploadifySettings不是函數

這裏是我的代碼:

$("#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. 已經在uploadify函數值「文件夾」,當用戶改變#folder的值(這是下拉改變)。

當我點擊選擇框時,什麼也沒有發生。如果我註釋掉行:

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

我得到的警報,車針,如果我離開了線它沒有做任何事情。

我諮詢過螢火蟲,看到我正在以下錯誤:

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

最後我做這與它的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