无法打开从akka http服务器下载的octet-stream文件。

问题描述 投票:0回答:1

我创建了一个Akka服务器,当用户向sever发布一些json结构时,服务器会发送文件,但我无法打开服务器发送的文件。

https:/imgur.comaIZ7JYhV

Akka http服务器(POST METHOD)=> http:/localhost:5004download。

val file = new File("srcmainresources "+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","*"),
               )
             )

JavascriptReact代码

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
           })
       })
    }
javascript reactjs scala akka akka-http
1个回答
0
投票

问题解决了!

我只是用vanilla Fetch API代替了axios,我不知道axios有什么问题。

© www.soinside.com 2019 - 2024. All rights reserved.