【问题标题】:Check if they have selected a file?检查他们是否选择了文件?
【发布时间】:2011-05-09 13:54:03
【问题描述】:

我正在使用 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()

..但这没有用。即使选择文件也总是空白。

【问题讨论】:

    标签: javascript jquery jquery-plugins uploadify javascript-framework


    【解决方案1】:

    您也可以通过 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;
        }
    

    【讨论】:

      【解决方案2】:

      关注这个。

      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
                                  }
                              }
      

      【讨论】:

      • 感谢您的回答。但是我的“垃圾”答案对我来说效果很好:)
      【解决方案3】:

      好的.....所以我决定用'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.');
      
          }
      
      });    
      

      【讨论】:

      • 我知道这篇文章已经快三年了,但感谢 Brett。我发现这真的很有帮助,事实上我用这种方法解决了我遇到的另一个问题,所以我想出了我自己的代码版本来解决多个文件附件。
      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2014-11-17
      • 2010-11-27
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多