【问题标题】:Upload file does not show up in $_POST or $_FILES, only $HTTP_RAW_POST_DATA上传文件不会显示在 $_POST 或 $_FILES 中,只有 $HTTP_RAW_POST_DATA
【发布时间】:2015-02-23 14:56:20
【问题描述】:

我正在尝试向我的 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'
    );

【问题讨论】:

    标签: javascript php jquery drupal drupal-6


    【解决方案1】:

    不确定这是浏览器问题还是什么,但我最终改用了 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;
    
            });
    

    【讨论】:

      猜你喜欢
      • 2017-06-10
      • 2013-01-06
      • 1970-01-01
      • 2011-12-12
      • 2012-09-29
      • 2011-04-23
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      相关资源
      最近更新 更多