当我尝试使用 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
关于javascript - TypeError : []. 文件未定义。在 jquery 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26773166/