0

I have an HTML form in my website and there is one file upload field in that form. When this form is submitted, I want to upload the selected file to the server and save it in the database. I have done the php part of uploading a file but I can not figure out the way how to send my file to that api that I created? I tried doing this:

var myFile = $("#picture_field").files[0];

and then send this variable as a parameter of my ajax request but I get the error which says:

TypeError: $(...).files is undefined

sallu-tech
  • 77
  • 2
  • 10
  • Does this answer your question? [Get data from file input in JQuery](https://stackoverflow.com/questions/12281775/get-data-from-file-input-in-jquery) – EinLinuus Mar 09 '21 at 17:15
  • @EinLinuus Quick question. I want to send data to my api in a non-json way from my ajax request because in my api, I am doing $_POST['param1'] and I don't want to use json_decode(receivedData); So how can I do that? I want to keep receiving the data in my php file like this: $_POST['param1'] and so on. – sallu-tech Mar 09 '21 at 18:51
  • $.ajax({ url: "url-here.php", method: 'POST', data: {name: 'John', age: 20} }) the data object is the data passed as post data – EinLinuus Mar 09 '21 at 20:46
  • @EinLinuus I am doing exactly like this but it's not working. – sallu-tech Mar 10 '21 at 08:55

1 Answers1

0
$('#picture_field').prop('files')[0]

Above line does exactly what I was looking for.

sallu-tech
  • 77
  • 2
  • 10