Jquery Ajax PHP无法获取上传的文件名

I am trying to display this "echo $_FILES['userfile']['name'];" on browser console but unfortunately I got this "function File() { [native code] }"

Here is my jquery code

<?= 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个回答



试试这个</ p>

  function uploadImage(){
//发送formData

var formData = new FormData($(“#userfile”)[0]);

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

$ .ajax({\ n 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 浏览器不支持FormData API!使用IE 10或更高版本!“);
}
}
</ code> </ pre>

注意:而不是' http://localhost/selection/index.php/CI_Inner/importResult '使用直接网址baseUrl +'importResult ”,</ p>


示例:url:
' http ://localhost/selection/index.php/CI_Inner/importResult ',</ p>

url:baseUrl +'importResult',</ p>
</ blockquote> \ n </ div>

展开原文

原文

Try this

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

Note: instead of 'http://localhost/selection/index.php/CI_Inner/importResult' using direct URL baseUrl + 'importResult',

example: url : 'http://localhost/selection/index.php/CI_Inner/importResult',

url: baseUrl + 'importResult',

doukui9491
doukui9491 回答更新检查一次
2 年多之前 回复
doudi5291
doudi5291 点击这里stackoverflow.com/questions/26340985 / ...
2 年多之前 回复
doumianfeng5065
doumianfeng5065 我得到此错误ReferenceError:未定义formData
2 年多之前 回复



最后我通过从下面的代码中删除参数'File'解决了这个问题</ p>

 <  code> var formdata = new FormData(); 
formdata.set('userfile',$('input [name =“userfile”]')[0] .files [0],** File **); \ n $ .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);
}
</ code> </ pre>

现在它可以正常使用下面的代码</ p>

  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',\ n dataType:'html',
contentType:false,
processData:false,
data:formdata,
success:function(data){
console.log(data);

}
</ code> </ pre>
</ div>

展开原文

原文

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

                }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐