0

I have sending data from ajax

Html element is given here

<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 element is given here. I have get files for onchange event in jquery

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
                        });
                    }

Here file is stored in file variable. I can see in console.

I have receiving data in php

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

But it shows

Undefined index: file in C:\xampp\htdocs\apk2tpk\wp-content\plugins\download-monitor\admin\extensionfile.php
Mohaideen Ismail
  • 314
  • 4
  • 21
  • [This](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) might help. Does your form have the correct enctype attribute? – Alex Jul 14 '14 at 08:41

1 Answers1

0

I tested it on my own machine, and I found out that you have to access the file from the form like this: e.target[0].files[0], and then in PHP you can access it like this: $_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
    }
});
Jonan
  • 2,485
  • 3
  • 24
  • 42