javascript - TypeError : []. 文件未定义。在 jquery 文件中

标签 javascript jquery

当我尝试使用 jquery post 上传图像文件时。 我的 jquery 用于下面给出的帖子。

$(document).ready(function(){

    $('#submit_img').bind("click",function(){
        if(validateProfile()){
            //alert(img);
            var img = new FormData();
            img.append('file',[].files[0]);
            //$('#file_upload')[0].files[0];
        $.post(profileUrl,{'api_key':apiKey,
                 'api_secret':apiSecret,
                 'id_user': userid,
                 'profile_image':img,
                 },function(data){
                     $.each($.parseJSON(data),function(key,value){

                         var stStatus = value.status; 
                         var stText = value.status_text;
                         alert(stText);
                         //$("#error_username").html(stText);    

                    });     
           });
        }
    });
});

当按下提交按钮时给出类似“TypeError: [].files is undefined”的错误我不知道确切的原因是什么。我是 jquery 的新手。

最佳答案

尝试将 [].files[0] 替换为 $( '#file' )[0].files[0],其中 #file 中的文件是您的输入元素 ID

https://stackoverflow.com/a/8319599/4221558

关于javascript - TypeError : []. 文件未定义。在 jquery 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26773166/

相关文章:

javascript push() 没有按预期工作

javascript - 在 jQuery .each 循环中比较字符串

javascript - 检测使用 javascript 单击哪个链接将用户带到特定页面

javascript - 如何在文本结束后放置冒号

javascript - HTML5 Canvas 显示图像

javascript - 使用 jQuery 在另一个文本框中输入文本时更新一个文本框

javascript - Jquery - 如何以 html 形式存储选中的项目并将其返回到数组中?

javascript - 如何使用绑定(bind)数据作为 polymer 中的函数输入

javascript - 单元测试 AngularJS 指令 : scopes not updating?

javascript - HTML5 Canvas - 按类在所有 Canvas 元素中绘制相同的内容