gpt4 book ai didi

javascript - 将 Blob 数据作为 MultipartFile 从 Angular6 客户端发送到 Spring Boot API

转载 作者:太空狗 更新时间:2023-10-29 19:32:23 25 4
gpt4 key购买 nike

我正在尝试将音频作为多部分文件上传到 Spring Boot API。我从来没有在 Angular2.x 应用程序中做过任何类似的事情,所以完全不确定我的方法是否正确。我一直从 api 收到 500 错误,我想可能是我的音频数据没有以正确的格式发送……我在这里遗漏了什么吗?我查看了同一 API 的 gui 代码,该 API 在该 gui 中使用 javaFX 完成,音频被记录,保存为 wav,然后发送到 api 端点。我应该先保存录音的 .wav 文件,然后将其读取/发送到 API,还是可以像我目前所做的那样使用 Blob?

我的代码:

the Kotlin/spring boot endpoint:@PostMapping("/vb/Verify")
fun verify(@RequestParam("Audio") file:MultipartFile,
@RequestParam(name = "SessionID") sessionId: String,
@RequestParam(name = "Risk") risk: String,
@RequestParam(name = "Strategy") strategy:String,
@RequestParam(name = "AudioType") audioType:String,
@RequestParam(name = "Channel") channel:String):VerifyResponseDTO {

val audioBytes = StreamUtils.copyToByteArray(file.inputStream)
return vbService.verify(sessionId, audioBytes, audioType, strategy, channel, Risk.byCode(risk))

}


my angular component code:


constructor(private restService: RestService) {
const onSuccess = stream => {
this.mediaRecorder = new MediaRecorder(stream);
this.mediaRecorder.onstop = e => {
const audio = new Audio();
const blob = new Blob(this.chunks, {type : 'audio/wav'});
this.chunks.length = 0;
audio.src = window.URL.createObjectURL(blob);
this.verify(blob);
audio.load();
audio.play();
};

this.mediaRecorder.ondataavailable = e => this.chunks.push(e.data);
};

navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);

navigator.getUserMedia({ audio: true }, onSuccess, e => console.log(e));
}

verify(blob: Blob) {
this.restService.startSession()
.subscribe((res) => {
console.log(res);
this.restService.verify(blob)
.subscribe((res) => {
console.log(res);
}, (err) => {
console.log(err);
});
}, (err) => {
console.log(err);
});
}



my Angular Service verify function:
verify(blob: Blob): Observable<any> {
let formData:FormData = new FormData();
formData.append("SessionID", "neld02");
formData.append("Strategy", "Phrase");
formData.append("AudioType", "Header");
formData.append("Risk", "Lo")
formData.append("Channel", "Smart")
formData.append('Audio', blob);
formData.append("Text", "");


return this.hc.post(`${this.api}/Verify`, formData);
}

最佳答案

引用@RequestParam("Audio") file:MultipartFile,

我有类似的问题,你会尝试将 blob 映射到 file() 对象吗?

  const file = new File([blob], 'audio.wav');
const formData = new FormData();
formData.append('File', file, file.name); // file.name was mandatory for us (otherwise again an error occured)

关于javascript - 将 Blob 数据作为 MultipartFile 从 Angular6 客户端发送到 Spring Boot API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52577105/

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