0

I have an input tag of type file. I use this to get a file from my local disc. What I need is a way to get and hold that file so I can POST it to a server.

<input type='file'> 

//I now want to hold this file and pass this to this ajax post method

$.ajax({
  type: "POST",
  url: url, //I have URL, this isn't a problem
  data: data, // do I place response here ? 
  success: function(){ alert('file posted')
  dataType: dataType // ? 
})
thatOneGuy
  • 9,977
  • 7
  • 48
  • 90

1 Answers1

0

You have to post form data to the server through ajax. Try Following.

$.ajax({
    url: url,
    type: "POST",
    data: new FormData('#yourForm'),
    contentType: false,
    cache: false,
    processData:false,
    success : function(){
                  // success message.
              }
});

#yourForm is your form. if you are using form submit event then replace this by 'this' keyword. So the formData is your form's data. and fetch file input by name on server.

Hemant Sankhla
  • 923
  • 12
  • 23