简体   繁体   English

将 ByteArrayResource(从 java)接收到 Blob(到 javascript)

[英]Receiving ByteArrayResource( from java) into Blob (into javascript)

any help to my problem would be really appreciated.对我的问题有任何帮助将不胜感激。 Here is the context:这是上下文:

I am currently sending a zip file from java RestControler:我目前正在从 java RestControler 发送一个 zip 文件:

public ResponseEntity<Resource> download(@RequestBody ProjectDTO projectDTO) {
    Project project = ProjectDTO.toProject(projectDTO);
    byte[] out = initApplicationService.download(project);
    ByteArrayResource resource = new ByteArrayResource(out);
    return ResponseEntity
      .ok()
      .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + getZipFilename(project))
      .contentType(MediaType.parseMediaType("application/octet-stream"))
      .contentLength(out.length)
      .header("X-Suggested-Filename", getZipFilename(project))
      .body(resource);
  }

This works really well because i can dowload it through swagger.这非常有效,因为我可以通过 swagger 下载它。

But on plain javascript front, i can't figure out what i am missing: primary:但是在普通的 javascript 前面,我不知道我错过了什么:主要的:

const download = async (): Promise<void> => {
      if (project.value.folder !== '') {
        await projectService
          .download(toProject(project.value))
          .then(response => {
              const url = window.URL.createObjectURL(new Blob([response],{type:'application/octet-stream'}));
              const link = document.createElement('a');
              link.href = url;
              link.setAttribute('download', project.value.baseName + '.zip');
              document.body.appendChild(link);
              link.click();
          })
          .catch(error => logger.error('Downloading project failed', error));
      }
    };

calling secondary:二次调用:

async download(project: Project): Promise<ArrayBuffer> {
    const restProject: RestProject = toRestProject(project);
    return this.axiosHttp
      .post<ArrayBuffer, RestProject>('api/projects/download', restProject)
      .then(response => new Uint8Array(response.data));
  }

It dowloads well a zip but well opening it i got the following error: "An error occured during archive loading" Thanks to any help:)它很好地下载了 zip 但很好地打开它我收到以下错误:“存档加载期间发生错误”感谢任何帮助:)

I added the responseType in post request and i also removed Uint8Array convertion and it worked fine.我在 post 请求中添加了 responseType,我还删除了 Uint8Array 转换,它工作正常。

return this.axiosHttp
      .post<ArrayBuffer,RestProject>('api/projects/download', restProject,{ responseType: 'blob' })
      .then(response => response.data);

I guess that when receiving the datas, axios needs to know what is coming to convert correctly it in the proper format.我想当接收数据时,axios 需要知道将要以正确格式正确转换的内容。 on the fly.在飞行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM