我已经创建了akka服务器发送文件时,用户后一些json结构到服务器,但我不能打开的文件是由服务器发送
https://imgur.com/a/IZ7JYhV
Akka http服务器(POST方法)=〉http://localhost:5004/download
瓦尔文件=新文件(“源/主/资源/"+路径)
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
})
})
}
1条答案
按热度按时间huwehgph1#
问题解决了!!
我只是使用了vanilla Fetch API而不是axios,我不知道axios有什么问题