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 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来