function SpeechKit(link) {
console.log(link);
var data = null;
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://stt.api.cloud.yandex.net/speech/v1/stt:recognize",true);
xhr.setRequestHeader("Authorization", "Api-Key ........");
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.setRequestHeader("Accept", "/");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Postman-Token", "9d5bfa....");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
}
file_put_contents($audioFileName, base64_decode($file));
<input type="file" id="myFileField" name="myFile" />
var formData = new FormData();
formData.append("myFile", document.getElementById("myFileField").files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", "myServletUrl");
xhr.send(formData);
// Это базовый пример с комментариями. Функция upload - это то, что вы ищете:
// Select your input type file and store it in a variable
const input = document.getElementById('fileinput');
// This will upload the file after having read it
const upload = (file) => {
fetch('http://www.example.net', { // Your POST endpoint
method: 'POST',
headers: {
// Content-Type may need to be completely **omitted**
// or you may need something
"Content-Type": "You will perhaps need to define a content-type here"
},
body: file // This is your file object
}).then(
response => response.json() // if the response is a JSON object
).then(
success => console.log(success) // Handle the success response object
).catch(
error => console.log(error) // Handle the error response object
);
};
// Event handler executed when a file is selected
const onSelectFile = () => upload(input.files[0]);
// Add a listener on your input
// It will be triggered when a file will be selected
input.addEventListener('change', onSelectFile, false);