I have several areas where I use AJAX to submit text fields.
var name = $("input#name").val();
$.ajax({
type: "POST",
url: "bin/process.php",
data: "&name=" + name,
success: function() {
//handle response here
}
});
However, I can't figure out how to do this if the input type is a file? Doing
var name = $("input#file").val();
doesn't seem to work..
You can upload the selected file by creating a FormData class and passing it to Axios' post() function. const input = document. querySelector('#my-input'); const formData = new FormData(); formData. append('myFile', input.
2.1 Create a Javascript FormData object from a form. $. ajax({ type: "POST", enctype: 'multipart/form-data', processData: false, // Important!
File upload is not possible through AJAX. You can upload file, without refreshing page by using IFrame .
By default, jQuery cannot POST a form via AJAX if it contains a upload field.
You can try this plugin: http://jquery.malsup.com/form/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With