2

I try to submit contact information from my webpage. I am using ajax to post the form data (Name,Subject,etc.., with attachment file).

The form values of

  1. Name,subject,etc.., these are string type

  2. Attachment file only object type

In my first case is:

var Name =$("#Full_Name").val()
var Subject =$("#Subject").val()
var Message =$("#Message").val()
var Email =$("#Email_Address").val()
var Phone =$("#phoneNumber").val()
var userfile =$("#uploadBtn").files[0]

var Address =$("#Address").val()
var City =$("#City_Name").val()
var Pincode =$("#City_code").val()
var State =$("#State_Name").val()
var Country =$("#Country_Name").val()

the above code to post all datas correctly without userfile (attachment file )

In my second case:

I replace

var userfile =$("#uploadBtn").files[0] 

this one to

var userfile = new FormData();

$.each($('#uploadBtn')[0].files, function(i, file) {
      userfile.append('file-'+i, file);
});

after add this line for posting upload file to server

But server get the result is only the format of [object:object]

Why server get this result? How to get the form data values with upload file

My code is:

In HTML:

<form action="#" name="f" enctype="multipart/form-data" id="myform" >

In Javascript:

    $.ajax({
               async:true,
               url: "/cgi-bin/servercall_upload.cgi",
               type: "get",
               processData: false,
               contentType: false,
               traditional: true,
               data: {
                     Name:Name,
                     Subject:Subject,
                     Message:Message,
                     Email:Email,
                     Phone:Phone,
                     userfile:userfile,
                     Address:Address,
                     City:City,
                     Pincode:Pincode,
                     State:State,
                     Country:Country

               }
    });

How do I solve this Problem?

laxmi
  • 29
  • 2
  • what is your exact problem, please elaborate it and tell the result what you have got still now.And what type of file you are trying to upload? – learner Jul 16 '16 at 08:31
  • Please review my edited version of question. server gets the output value is object. In my first case server does not receive the whole data specifically userfile. but my second case server receive the [object:object] data only – laxmi Jul 16 '16 at 09:49
  • Maybe duplicate of this [Sending multipart/formdata with jQuery.ajax](http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax) – Mario Santini Jul 16 '16 at 10:03
  • have you tried the above link @laxmi. Is it working or you still get errors? if yes please post the errors – learner Jul 16 '16 at 12:13
  • I am already tried above link but is not working, only server gets the output value is object. – laxmi Jul 26 '16 at 07:57

0 Answers0