2012-04-23 73 views
2

我使用的jQuery插件https://github.com/blueimp/jQuery-File-Upload最多加载文件。我只想使用上传按钮来显示选定的文件,并且只在表单提交时才上传文件。下面是我的代码:

<form id="myform" method="post" enctype="multipart/form-data"> 
       <span class="btn btn-success btn-mini fileinput-button"> 
        <i class="icon-plus icon-white"></i> 
        <span>Add files</span> 
        <input id="fileupload" type="file" name="files[]" multiple> 
        <button type="submit" id="start_uploads">Start uploads</button> 
       </span> 
    </form> 

我可以使用下面的代码创建列表:

$('#fileupload').fileupload({ 
     dataType: 'json', 
     add: function (e, data) { 
          $.each(data.files, function (index, file) { 
           //alert('Added file: ' + file.name); 
           $('<li/>').text(file.name).appendTo('#fileList'); 
              }); 

            }, 

           }); 

但是,当我提交表单我不想在$ _FILES或后续代码var_dump得到任何东西(文件);任何人都可以帮我解决我缺少的东西以及如何完成这个任务? 有一点要提到的是,如果我不使用js代码作为列表,那么获取具有所有选定文件的$ _FILES变量。

+1

你可以发布你的PHP代码,也文件的信息,请? – WolvDev 2012-04-23 07:37:22

+0

if(($ _ FILES)) var_dump($ _ FILES); 其他回应“Nothing”; – marifrahman 2012-04-23 07:53:40

+0

不过,U可以在这里查看整个代码:http://pastebin.com/BBZLaGJk – marifrahman 2012-04-23 07:56:08

回答

0
<?php if(!empty($_FILES)):?> 
    <?php echo json_encode($_FILES);?> 
<?php else:?> 
    <!DOCTYPE HTML> 
    <html> 
     <head> 
      <meta charset="utf-8"> 
      <title>jQuery File Upload Example</title> 
      <link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap.min.css"> 
      <!-- Generic page styles --> 

      <!-- Bootstrap styles for responsive website layout, supporting different screen sizes --> 
      <link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap-responsive.min.css"> 
      <!-- Bootstrap CSS fixes for IE6 --> 
      <!--[if lt IE 7]><link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap-ie6.min.css"><![endif]--> 
      <!-- Bootstrap Image Gallery styles --> 
      <link rel="stylesheet" href="http://blueimp.github.com/Bootstrap-Image-Gallery/css/bootstrap-image-gallery.min.css"> 
      <!-- CSS to style the file input field as button and adjust the Bootstrap progress bars --> 
      <link rel="stylesheet" href="css/jquery.fileupload-ui.css"> 
     </head> 
     <body> 
      <input id="fileupload" type="file" name="files[]" multiple> 
      <button id="sendFiles">Send</button> 
      <ul id="fileList"></ul> 
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script src="js/vendor/jquery.ui.widget.js"></script> 
      <script src="js/jquery.iframe-transport.js"></script> 
      <script src="js/jquery.fileupload.js"></script> 
      <script src="js/jquery.fileupload-ui.js"></script> 
      <script> 
       $(function() { 
        var filesData; 
        $('#fileupload').fileupload({ 
         dataType: 'json', 
         add: function (e, data) { 
          filesData = data; 
          $.each(data.files, function (index, file) { 
           $('<li/>').text(file.name).appendTo('#fileList'); 
          }); 

         }, 
         done: function (e, data) { 
          console.log(data); 
         } 

        }); 

        $('#sendFiles').on('click', function() { 
         if (filesData) { 
          filesData.submit(); 
         } 
        }); 
       }); 
      </script> 
     </body> 
    </html> 
<?php endif;?> 

基本上,你必须调用所述数据对象的方法submit在功能add。然后你会得到包含$_FILES作为json字符串的data对象。而且你不必使用实际的表单来通过这个插件发送文件,但是你可以用一个表单标签(就像它在你的代码中)包装输入,以便允许没有JavaScript的用户(它们是否存在?:))也发送文件。 如果您不想通过JavaScript发送文件,那么为了显示文件列表,您不必使用该插件。你可以得到有关使用文件清单https://developer.mozilla.org/en-US/docs/Web/API/FileList