上传文件不会显示在$ _POST或$ _FILES中,只显示$ HTTP_RAW_POST_DATA

时间:2015-02-23 14:56:21

标签: javascript php jquery drupal drupal-6

我正在尝试向Drupal 6自定义模块添加异步上传。但问题是,当上传发生时(我的php函数有一个断点)在$ _POST或$ _FILES中看不到任何数据。该文件的所有数据都放入$ HTTP_RAW_POST_DATA。我希望它在$ _FILES数组中。任何人都可以告诉我我做错了什么。或者至少如何使用$ HTTP_RAW_POST_DATA来处理后端的文件。这是我的代码:

在my_module_viewer.views.inc

<form  id='my_upload_form' enctype='multipart/form-data'  method='POST'>
     <input type='file' name= 'file_upload' id= 'file_upload' multiple>
     <input type='button' name ='file_upload_button' id ='file_upload_button' value= 'Upload' />
</form>

Java脚本:

 $('#file_upload_button').click(function(){
            var files = $( '#file_upload' )[0];
            var data = new FormData();
            jQuery.each(files.files, function(i, file) {
                data.append('file-'+i, file);
            });
            var request_timeout = 50000;
            var url =  Drupal.settings.basePath + 'my_module/cases/add_attachment';
            $.ajax({
                url: url,
                data:data,
                cache: false,
                contentType: false,
                processData: false,
                type: 'POST',
                beforeSend: function(xhr ){
                    $.blockUI({
                        message: "Uploading File.  Please Wait.",
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#333',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .9,
                            color: '#fff',
                            fontSize: '26px',
                            fontFamily: "'Helvetica Neue', Helvetica"
                        }
                    });
                },success: function(data) {                        

                    $.unblockUI();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {

                    $.unblockUI();
                }
            });
        });

正如我所提到的,这是用Drupal 6编写的。我不确定这是否相关,但这是我的菜单:

在my_module.module上找到

   $items['my_module/cases/add_attachment']= array(
        'page callback' => 'add_attachment',
        'access arguments' => array('add attachment for user'),
        'type' => MENU_CALLBACK,
        'access callback' => true,
        'file' => 'my_module_viewer.views.inc'
    );

1 个答案:

答案 0 :(得分:0)

不确定这是浏览器问题还是什么,但我最终使用的是XHR。像这样:

$('#file_upload_button').click(function(){
            var files = $( '#file_upload' )[0];               
            var data = new FormData();                
            var url =  "myPath";
            var xhr = new XMLHttpRequest();               
            var fileCount = 0;    

                jQuery.each(files.files, function (i, file) {
                    data.append('file-' + i, file)
                    fileCount++;
                });
                if (fileCount > 0) {                      


                    xhr.open('POST', url, true);
                    xhr.upload.onprogress = function(e) {    
                    };    

                    xhr.upload.onerror = function(e){
                        $.unblockUI();
                    };

                    xhr.upload.onloadstart = function(e){
                        showBlockUI("Uploading Files...")
                    };

                    xhr.addEventListener('readystatechange', function(e) {
                        if( this.readyState === 4 ) {
                            var result = this.responseText;


                            if(result){
                                result = JSON.parse(result);

                                }
                            }
                            $( '#file_upload').val(null);
                            $.unblockUI();    
                        }
                    });
                    xhr.send(data);
                }


            return false;

        });