从ajax传递php无法访问的数据

时间:2014-07-14 08:36:03

标签: php ajax

我从ajax发送数据

这里给出了Html元素

<input type="file" name="expansionfile" class="expansionfile" style="width:354px; margin:1px;" />

jQuery(document).ready(function(e) {
                        jQuery(".progress").hide();
                        jQuery('.expansionfile').on('change',prepareUpload);        
                    });

这里给出了Ajax元素。我在jquery

中获取了onchange事件的文件
function prepareUpload(event)
                    {
                        var file= event.target.files[0];
                        //var file = this.files[0];
                        jQuery(".progress").show();
                        var bar = jQuery('.bar');  
                        var percent = jQuery('.percent');

                        //var data = new FormData();
                        //data.append('file',file);
                        //var data= {"file":file};
                        formData = new FormData();
                        formData.append('file', event.target[0].files[0]);
                        var data = formData;
                        //data = JSON.parse(data);



                        jQuery.ajax({
                            url: '../wp-content/plugins/download-monitor/admin/extensionfile.php',
                            type: 'POST',
                            data:data,
                            cache: false,
                            enctype: 'multipart/form-data',
                            //dataType: 'json',
                            processData: false,
                            contentType: false,
                            success: function(data, textStatus, jqXHR)
                            {
                                    console.log('success');
                            },
                            error:function(jqXHR, textStatus, errorThrown){
                                console.log(errorThrown);
                            },
                            complete:function(){
                            },
                             xhrFields: {
                              onprogress: function (progress, position, total, percentComplete) {
                                var percentage = Math.floor((progress.total / progress.totalSize) * 100);
                                var percentVal = percentage + '%';  
                                 bar.width(percentVal)  
                                 percent.html(percentVal);
                                //console.log('progress', percentage);
                                if (percentage === 100) {
                                  //console.log('DONE!');
                                }
                              }
                            },
                            processData: false,
                            contentType: file.type
                        });
                    }

此处文件存储在文件变量中。我可以在控制台中看到。

我在php中收到了数据

$filename = $_POST['file'];
    var_dump($filename);

但它显示

Undefined index: file in C:\xampp\htdocs\apk2tpk\wp-content\plugins\download-monitor\admin\extensionfile.php

1 个答案:

答案 0 :(得分:0)

我在自己的机器上测试过,我发现你必须从这样的表格中访问文件:e.target[0].files[0],然后在PHP中你可以像这样访问它:$_FILES['file']

formData = new FormData();
formData.append('file', event.target[0].files[0]);
var data = formData;
jQuery.ajax({
    url: '../wp-content/plugins/download-monitor/admin/extensionfile.php',
    type: 'POST',
    data: data,
    cache: false,
    processData: false,
    contentType: false,
    success: function(data){
        //do whatever you want
    }
});