我有一个烧瓶 API。使用邮递员一切正常,但是当我尝试通过 fetch 发送文件时,它没有出现。
ImmutableMultiDict([])
@app.route('/voice_to_text', methods=['POST'])
def api_voice_to_text():
try:
check_model(model_dir)
audio = request.files.get('audio')
print(request.files)
filename = f'{audio.filename}_{random.randint(0, 100000)}.wav'
audio.save(os.path.join("audio", filename))
return jsonify({"result": transcript}), 200
except:
return jsonify({"result": "error"}), 400
<html>
<head>
<meta charset = "Utf-8">
</head>
<body>
<input id="audio-file" type="file" />
</body>
<script>
let audio = document.getElementById("audio-file").files[0];
let formData = new FormData();
formData.append("audio", audio);
setTimeout(testSizze, 5000);
async function testSizze(){
const res = await fetch("http://0.0.0.0:5000/voice_to_text", {
method: "POST",
body: formData
});
const json = await res.json();
console.log(json)
}
</script>
</html>