Poonam Poonam - 6 months ago 14
Javascript Question

Can't get inputType='file' data on form submit

In jQuery, when a function got success a form getting build. Now when I submit this form it gives all data except

inputType='file'
. I can't get it why this is happening

here is my jQuery code when form is creating

content += '<form method="POST" action="'+formURL+'" id="data" enctype="multipart/form-data">'+
'<input type="text" name="album_id" value="'+id+'">'+
'<input type="text" name="user_id" value="'+user_id+'">'+
'<input type="file" name="image" id="image_upload">'+
'<input type="submit" value="Submit">'+
'</form>';


Here form is getting submit

$("form#data").submit(function() {
var formData = new FormData($(this)[0]);
$.post($(this).attr("action"), formData, function(data) {
console.log(data);
});

return false;
});


I am sending this form data in a controller in cakephp.
In controller I get only input field data with
text
type only. But I need
file
type too.

Answer

please use jquery.form.js for file upload.

http://malsup.com/jquery/form/

<form method="POST" action="'+formURL+'" id="data" enctype="multipart/form-data" onsubmit="return submit_form();" >


function submit_form(){

    $('#data').ajaxSubmit({  
      method:'post',
      dataType:'json',    
      success: function(resp){

      }

    });
    return false;
  }
Comments