无法从9中的httpresponse读取头值

ghg1uchk  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(163)

我正在尝试下载一个zip文件,它位于服务器中。对于ui,使用angular9和backend作为spring引导。
在后台,我为http响应对象设置了自定义头值,如下所示

JreModel jre = findJre(FILE_NAME);
        String filePath =  jre.getLocation();
        File downloadFile= new File(filePath);
        byte[] isr = Files.readAllBytes(downloadFile.toPath()
                );
        ByteArrayOutputStream out = new ByteArrayOutputStream(isr.length);
        out.write(isr, 0, isr.length);

        response.setContentType("application/zip");
        // Use 'inline' for preview and 'attachement' for download in browser.
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + FILE_NAME);
        OutputStream os;
        try {
            os = response.getOutputStream();
            out.writeTo(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }else {
        response.addHeader("Content-Disposition",res.get());
    }

我想用下面的代码下载zip文件
休息服务.ts

buildJRE(jrePackage) : Observable<any>{
    return this.http.post(Jreconstants.BUILD_API_URL,jrePackage, {
      responseType: 'blob',
      observe: 'response'
    })
    .pipe(
      map((res: any) => {
        console.log("--"+res)
        return new Blob([res.body], { type: Jreconstants.RESPONSE_TYPE });

      })
    );
  }

组件.ts

buildCustomJRE(){

        if(this.checkItem.length==0){
          this.msg = Jreconstants.PACKAGE_ERROR;
          this.showAlert= true;

          return;
        }
        this.service.buildJRE(this.checkItem).subscribe(response => {
          if(response.size ===0){
            this.showAlert= true;
            this.msg =Jreconstants.DOWNLOAD_ERROR;
            return;
          }
          console.log(response.headers);
          var binaryData = [];
          binaryData.push(response);
          var url = window.URL.createObjectURL(new Blob(binaryData, {type: `${Jreconstants.RETURN_TYPE}`}));
          var a = document.createElement('a');
          document.body.appendChild(a);
          a.setAttribute('style', 'display: none');
          a.setAttribute('target', 'blank');
          a.href = url;
          a.download = "jrename";
          a.click();
          window.URL.revokeObjectURL(url);
          a.remove();

        }, error => {

          console.log(error);
        });

  }

我可以下载带有此代码的文件。但我的下载文件将是jrename.zip
现在我硬编码文件名

a.download = "jrename";

如何将头文件名设置为 a.download="" 当我试图从浏览器调试代码时,它显示了下面的输出

我的代码会有什么错误。我遗漏了什么吗。
我跟踪了这个stack:->Angular Spring启动文件下载
编辑1
服务.ts

buildJRE(jrePackage) : Observable<any>{
   return this.http.post(Jreconstants.BUILD_API_URL,jrePackage, {
      responseType: 'blob',
      observe: 'response'
    })
    .pipe(
      map((res: any) => {
        console.log("--"+res)
        //return new Blob([res.body], { type: Jreconstants.RESPONSE_TYPE});
        return {
          filename: res.filename,
          data: new Blob([res.body], { type: Jreconstants.RESPONSE_TYPE})
      };

      })
    );

休息服务.ts

buildCustomJRE(){

        if(this.checkItem.length==0){
          this.msg = Jreconstants.PACKAGE_ERROR;
          this.showAlert= true;

          return;
        }
        this.service.buildJRE(this.checkItem).subscribe(response => {
          if(response.size ===0){
            this.showAlert= true;
            this.msg =Jreconstants.DOWNLOAD_ERROR;
            return;
          }
          console.log(response.headers);
          var binaryData = [];
          binaryData.push(response);
          var url = window.URL.createObjectURL(new Blob(binaryData, {type: `${Jreconstants.RETURN_TYPE}`}));
          var a = document.createElement('a');
          document.body.appendChild(a);
          a.setAttribute('style', 'display: none');
          a.setAttribute('target', 'blank');
          a.href = url;
          a.download = response.filename;
          a.click();
          window.URL.revokeObjectURL(url);
          a.remove();

        }, error => {

          console.log(error);
        });

  }

文件名仍然没有定义。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题