HTML ::
<form id='anyid' method="post" enctype="multipart/form-data" >
<input type='file' name='image' />
</form>
JAVASCRIPT (JQUERY):
$("form#anyid").submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: 'https://anydomain.com/fileupload',
type: 'POST',
data: formData,
success: function (data) {
console.log('we are done with upload');
},
cache: false,
contentType: false,
processData: false
});
});
here main part is
var formData = new FormData(this);
new FormData will create object with all form field of passed form element.
even you can append the file as need like this
formData.append(name, blob, fileName)
One can use any server-side code to file get uploaded at server