gpt4 book ai didi

javascript - 获取音频文件并将其加载到表单附件中

转载 作者:行者123 更新时间:2023-11-28 10:40:00 25 4
gpt4 key购买 nike

假设我有一个用户在浏览器中录制的音频文件。我愿意

  1. 获取文件
  2. 将文件加载到表单的输入

一些较旧的帖子讨论了由于安全问题这是不可能的,但现在根据这个答案这是可能的:how to set file input after file drop

表格:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">

<div class='col-sm-6' style="padding-left:0px;" >
<form action="/main/" method="post" id="my_form" enctype="multipart/form-data">
{% csrf_token %}

<div> <input type="file" name="audio" id="id_audio" /> </div>


<button type="submit" disabled style="display: none" aria-hidden="true"></button>
<input class="btn btn-success" type="submit" name="submit" value="Gem" />
</form>
</div>

音频文件:

<ol id="recordingsList">
<li id="myfile">
<audio controls="" src="blob:http://127.0.0.1:8000/99888"></audio>
myaudiofile.wav
</li>
</ol>

创建音频控件和下载文件链接的函数,我想将文件附加到表单中的 id_audio 输入:

<script>

function createDownloadLink(blob) {

var url = URL.createObjectURL(blob);
var au = document.createElement('audio');
var li = document.createElement('li');
var link = document.createElement('a');

$(li).attr('id', 'recordedaudio-id');
$(link).attr('id', 'savetodisk');

//name of .wav file to use during upload and download (without extendion)
var filename = new Date().toISOString();

//add controls to the <audio> element
au.controls = true;
au.src = url;

//save to disk link
link.href = url;
link.download = filename+".wav"; //download forces the browser to donwload the file using the filename
link.innerHTML = "Save to disk";

//add the new audio element to li
li.appendChild(au);


//add the filename to the li
li.appendChild(document.createTextNode(filename+".wav "))

//add the save to disk link to li
li.appendChild(link);

//upload link
var upload = document.createElement('a');
upload.href="#";
upload.innerHTML = "Upload";
upload.addEventListener("click", function(event){
var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
if(this.readyState === 4) {
console.log("Server returned: ",e.target.responseText);
}
};
var fd=new FormData();
fd.append("audio_data",blob, filename);
xhr.open("POST","upload.php",true);
xhr.send(fd);
})
li.appendChild(document.createTextNode (" "))//add a space in between
li.appendChild(upload)//add the upload link to li


recordingsList.appendChild(li);

var fileElement = $('#myfile');
var audioFormElement = $('#id_audio');

// get the audio file from fileElement
// set audio file in audioFormElement

}


</script>

最佳答案

基于 MediaRecorder API ( https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder ),可能的解决方案:

将其存储在本地:

<script>
var handleSuccess = function(stream) {
var link = document.createElement('a');
link.textContent = 'Download';
link.target = '_blank';
link.setAttribute("download", 'audio_1.ogg');

if (window.URL) {
link.href = window.URL.createObjectURL(stream);
} else {
link.href = stream;
}

// Append <a> to where you want to.
};
</script>

使用 XMLHttpRequest 上传:

var chunks = [];
var handleSuccess = function(stream) {
var mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
};
mediaRecorder.requestData();
var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });

var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
if(this.readyState === 4) {
console.log("Server returned: ", e.target.responseText);
}
};
var formData = new FormData();
formData.append("audio_data", blob, 'audio_1.ogg');
xhr.open("POST", "upload.php", true);
xhr.send(formData);
}

关于javascript - 获取音频文件并将其加载到表单附件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53439467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com