I am testing some API functions through localhost using an HTML website with ajax to make this api calls, everything works great till i bumped to this api call:
localhost/uploadFile?file=?????&userID=123421
I have searched everywhere about a way to upload a file and send it with API but could not find anything helpful, what I know about this API call(uploadFile) is that it can only be a POST request.
So this is what I do:
HTML Code:
<form method="post" id="form">
<input type="file" name="file" id="file">
<button type="submit" onclick="check(this.form)" id="submitbtn" >Send</button>
</form>
Ajax Script:
function check(form){
$.ajax({
url: "http://localhost/uploadFile&file="+document.getElementById("file").value+"&userID=123421",
type: "POST",
dataType: "json",
success: function (result) {
switch (result) {
case true:
alert(result);
break;
default:
alert(result);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
I found this tutorial to use ajax to upload a file but it only saves it to the server how can I send it with the api? The mystery is in the parameter file what can I save there?? I printed the value of document.getElementById("file").value
and it's just somthing like C:/fakepath/fileName.txt
, If you have any idea how this works would be very helpful I am new to ajax and I am open to other web programming languages, except php for some api reasons...