0

我正在使用jQuery插件“uploadify”&我想要做的就是在上传开始后隐藏上传按钮,但是如果他们在选择文件之前点击它,它仍然会隐藏它。检查他们是否选择了一个文件?

这里是我的提交功能:

$("#add_list").submit(function(){ 

    // Set new list id 
    $("#filename").uploadifySettings('scriptData', { 'new_list_id': $('#new_list_id').val() }); 

    // Hide upload button 
    $("#upload_button").hide();  

    // Trigger upload 
    $("#filename").uploadifyUpload(); 

}); 

有没有一种方法,我可以得到的文件名字段的值?我试过..

$("#filename").val() 

..但没有奏效。即使在选择文件时也始终保持空白。

回答

1

好的.....所以我决定用'onSelect'事件来更新一个隐藏的表单域值;通过这种方式,当他们选择一个文件时,我可以更新该值以表明他们已经选择了一个文件;然后在触发上传之前检查此值。如果上传出现问题或者用户删除文件,我会在触发'onCancel'事件时将值更新为空白值。

下面是相关的代码,如果它可以帮助别人..

'onComplete': function(event, ID, fileObj, response, data) { 
     if (response != 'OK') { 
      // Cancel upload 
      $("#filename").uploadifyCancel(ID);    
      // Show upload button 
      $("#upload_button").show();   
      // Output error message 
      alert(response); 
     } else { 
      // Submit secondary form on page 
      document.finalize.submit(); 
     } 
    }, 
    'onError': function(event,ID,fileObj,errorObj) { 
     // Cancel upload 
     $("#filename").uploadifyCancel(ID); 
     // Format error msg 
     var error_msg = errorObj.type + '. Error: ' + errorObj.info + '. File: ' + fileObj.name; 
     alert(error_msg); 
    }, 
    'onSelect': function(event,ID,fileObj) { 
     // Update selected so we know they have selected a file 
     $("#selected").val('yes'); 
    }, 
    'onCancel': function(event,ID,fileObj,data) { 
     // Update selected so we know they have no file selected 
     $("#selected").val(''); 
    } 
}); 
$("#add_list").submit(function(){ 

    var selected = $("#selected").val(); 

    if (selected == 'yes') { 

     // Set new list id 
     $("#filename").uploadifySettings('scriptData', { 'new_list_id': $('#new_list_id').val() }); 

     // Hide upload button 
     $("#upload_button").hide();  

     // Trigger upload 
     $("#filename").uploadifyUpload(); 

    } else { 

     alert('Please select a file to upload.'); 

    } 

});  
+0

我知道这个职位几乎是三岁但由于布雷特。我发现这实际上对我使用这种方法非常有帮助,因为我遇到了不同的问题,所以我想出了自己的代码版本来解释多个文件附件。 – corix010 2013-11-06 19:33:59

0

关注这个。

function submitForm() 
         { 
          var html = document.getElementById('file_uploadQueue').innerHTML; 
          if(html.length > 0) 
           { 
$('#file_upload').uploadifyUpload($('.uploadifyQueueItem').last().attr('id').replace('file_upload','')); 
          } 
          else 
          { 
          alert('choose file to upload'); 
// or you can submit the form. If uplodify is optional for u 
          } 
         } 
+0

感谢您的回答。但我的“垃圾”答案对我来说工作得很好:) – Brett 2012-01-30 13:55:55

0

你也可以通过AJAX调用这个php函数来查看是否有任何东西被上传。 (我将上传的文件,一组文件夹上传后,所以这对我的作品非常好;))

/* 
    * returns the number of files in the tmp folder 
    * @return number 
    */ 
    public function countTmpFiles() 
    { 

     $source = "path/to/your/foler"; //here are the uploaded files 
     $files = scandir($source); 
     $result = 0; 
     foreach($files as $file) 
     { 

      if (in_array($file, array(".",".."))) 
      { 
       continue; 
      } 

      $result++; 

     } 
     return $result; 
    } 
相关问题