dpppic5186 2015-02-23 14:56
浏览 32
已采纳

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

I am trying to add an async upload to my Drupal 6 custom module. The problem is, though, that when the upload occurs (I have a break point on my php function) no data can be seen in $_POST or $_FILES. All the data for the file gets put into $HTTP_RAW_POST_DATA. I would expect it to be in the $_FILES array. Can anyone tell me what I'm doing wrong. Or at least how to use $HTTP_RAW_POST_DATA to process the file on the backend. Here is my code:

On 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 Script:

 $('#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();
                }
            });
        });

As I mentioned, this is being written in Drupal 6. I'm not sure if this is relevant, but here is my menu for that task:

Found on 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条回答 默认 最新

  • doumu6941 2015-02-26 16:09
    关注

    Not sure if this was a browser issue or what, but I ended up using XHR instead. Like this:

    $('#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;
    
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘