jQuery Ajax PHP无法获取上载的文件名

时间:2018-07-25 03:42:25

标签: php jquery ajax

我试图在浏览器控制台上显示此“ echo $ _FILES ['userfile'] ['name']; ”,但是很遗憾,我得到了这个” function File(){[本机代码]}

这是我的jquery代码

<?= form_open_multipart('',' id="importform" method="POST" ');?>

    <div><input type="file" name="userfile"></div><button type="submit>upload</button>
var formdata = new FormData();
            formdata.set('userfile',$('input[name="userfile"]')[0].files[0],File);
            $.ajax({
                url:'http://localhost/selection/index.php/CI_Inner/importResult',
                type: 'POST',
                dataType: 'html',
                contentType: false,
                processData: false,
                data: formdata,
                success: function(data){
                    console.log(data);
                }

2 个答案:

答案 0 :(得分:0)

尝试一下

function uploadImage() {
         // send the formData
        var formData = new FormData( $("#userfile")[0] );

    if (typeof formData !== 'undefined') {



        $.ajax({
            url : 'http://localhost/selection/index.php/CI_Inner/importResult',  // Controller URL
            type : 'POST',
            data : formData,
            async : false,
            cache : false,
            contentType : false,
            processData : false,
            success : function(data) {
                successFunction(data);
            }
        });

    } else {
       message("Your Browser Don't support FormData API! Use IE 10 or Above!");
    }   
}

注意:使用直接URL baseUrl +'importResult'代替'http://localhost/selection/index.php/CI_Inner/importResult',

  

示例:url:   'http://localhost/selection/index.php/CI_Inner/importResult'

     

url:baseUrl +'importResult',

答案 1 :(得分:0)

Eventually I solved this by just removing the parameter 'File' from my code below

var formdata = new FormData();
        formdata.set('userfile',$('input[name="userfile"]')[0].files[0],**File**);
        $.ajax({
            url:'http://localhost/selection/index.php/CI_Inner/importResult',
            type: 'POST',
            dataType: 'html',
            contentType: false,
            processData: false,
            data: formdata,
            success: function(data){
                console.log(data);
            }

And now it works fine with the code below

var formdata = new FormData();
            formdata.set('userfile',$('input[type=file]')[0].files[0]);
            $.ajax({
                url : 'http://localhost/selection/index.php/CI_Inner/importResult',
                type: 'POST',
                dataType: 'html',
                contentType: false,
                processData: false,
                data: formdata ,
                success: function(data){
                    console.log(data);

                }