1

When i try to upload an image file using the jquery post. my jquery for post given below.

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

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

When pressing the submit button give the error lke this "TypeError: [].files is undefined" I dont know the what the exact reason . I am new to jquery.

jyothish
  • 21
  • 1
  • 7

2 Answers2

2

try replacing [].files[0] with $( '#file' )[0].files[0] where file in #file is your input element id

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

Community
  • 1
  • 1
shaN
  • 798
  • 12
  • 23
  • error Like this "NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object . jquery-1.4.1.min.js:130" – jyothish Nov 06 '14 at 07:43
  • yes. at that time i use this "var fd = new FormData(); fd.append( 'file', input.files[0] );" – jyothish Nov 06 '14 at 07:58
0

Line 6, second argument of the append function, it appears you've accidentally erased your reference to fetching the input element.

So JavaScript is interpreting [] as create a new array, and then trying to access files on it.

DivinusVox
  • 1,133
  • 2
  • 12
  • 27