【问题标题】:can't open octet-stream files which is downloaded from akka http server无法打开从 akka http 服务器下载的八位字节流文件
【发布时间】:2020-06-07 13:51:13
【问题描述】:

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

https://imgur.com/a/IZ7JYhV

Akka http 服务器(POST 方法)=> 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
           })
       })
    }

【问题讨论】:

  • 您能否将问题隔离到前端或后端代码?例如,尝试使用curl 向服务器发出相同的请求。

标签: javascript reactjs scala akka akka-http


【解决方案1】:

问题解决了!!

我只是用vanilla Fetch API而不是axios,我不知道axios有什么问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多