Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload using Jquery ajax without form

Here is mycode

function addPackage(elem)
{
    
    var dataimg = new FormData();
    dataimg.append('', $("#browseimg"+elem).prop('files')[0]);
    var startdate=$("#from_date"+elem).val();
    var enddate=$("#to_date"+elem).val();
     $.ajax({
      url: "addpackage/",
      type:"post",
      contentType:false,
      data:{startdate:startdate,enddate:enddate,packageid:elem,img:dataimg},
      success: function(data) {
      }
    });
}

I tried post method ajax to upload image and input field data without form. In ajax call it showing [object object]. How to post image with input field without form in jquery ajax?

like image 479
saravanan mp Avatar asked Dec 25 '22 11:12

saravanan mp


1 Answers

You can do it like following. Hope this will help you.

function addPackage(elem)
{
    var dataimg = new FormData();
    dataimg.append('startdate', $("#from_date"+elem).val());
    dataimg.append('enddate', $("#to_date"+elem).val());
    dataimg.append('packageid', elem);
    dataimg.append('img', $("#browseimg"+elem)[0].files[0]);

    $.ajax({
        url: "addpackage/",
        type:"post",
        cache : false,
        contentType : false,
        processType : false,
        data: dataimg,
        success: function(data) {
        }
    });
}
like image 76
Ibrahim Khan Avatar answered Dec 27 '22 02:12

Ibrahim Khan