<form enctype="multipart/form-data" action="" method="post" class="create-form">
<input type="hidden" name="author" value="{$user.uid}">
<audio class="audio-player" controls="" src="blob:https://site.ru/ca854ba4-af79-4a4a-834f-ac8f009ebd01" autoplay=""></audio>
<button type="submit">Сохранить</button>
</form>
async function createFile(){
let response = await fetch(uri);
let data = await response.blob();
let metadata = {
type: 'audio/mpeg-3'
};
let file = new File([data], "questionAudio.mp3", metadata);
const formData = new FormData();
formData.append('audio-file', file);
formData.append('action', 'sendBlob');
return $.ajax({
url: document.location.href,
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
var res = JSON.parse(data, true);
resField.val(res);
}
});
}
createFile();
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('form.create-form').addEventListener('submit', async function() {
var form = this;
event.preventDefault();
var audio = await (await fetch(form.querySelector('audio').src)).blob();
var form_data = new FormData(form);
form_data.append('file', audio, 'FileName.mp3');
var response = await (await fetch(form.action, {
method: 'POST',
body: form_data
})).text();
console.log('Ответ сервера:', response);
form.outerHTML = 'Ответ сервера:<br>' + response;
});
});
</script>