无法打开从 AKKA HTTP 服务器下载的八进制流文件



我已经创建了 akka 服务器,当用户将一些 JSON 结构发布到服务器时发送文件,但我无法打开服务器发送的文件

https://i.stack.imgur.com/leyvf.jpg

Akka http server (POST METHOD( => http://localhost:5004/download

val file = new File("src/main/resources/"+path(

HttpResponse(StatusCodes.OK,entity=HttpEntity.fromFile(ContentTypes.`application/octet-stream`,file = file)).withHeaders(
scala.collection.immutable.Seq(
RawHeader("Content-Disposition",s"attachment; filename=${file.getName}"),
RawHeader("Access-Control-Expose-Headers","Content-Disposition"),
RawHeader("Access-Control-Allow-Origin","*"),
)
)

Javascript/React 代码

downloadFile = ()=>{
const tokenData = {
token:this.state.token,
password:this.state.password
}
axios.post("http://localhost:5004/download",tokenData).then(res=>{
var a = document.createElement('a');   
console.log(res.data)
var blob = new Blob([res.data], {type:"application/octet-stream"});
a.href = window.URL.createObjectURL(blob);
a.download = res.headers["content-disposition"].split("=")[1];
a.click();
}).catch(res=>{
this.setState({
error:true
})
})
}

问题解决了!!

我只是使用了香草 Fetch API 而不是 axios,我不知道 axios 有什么问题

最新更新