微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular 7-Spring Boot:将docx转换为Blob

如何解决Angular 7-Spring Boot:将docx转换为Blob

Angular 7前面板:

component.ts文件

this.myServices.getDocument(item.idaiworkflow,this.authService.getCurrentUser().username)
        .subscribe(res2 => {
          this.blob = new Blob([res2],{type: 'application/msword'}),console.log(res2);
          this.loading = false;
          },

service.ts文件

getDocument(id,user) {
    return this.http.get<any>(this.apiUrl + '/api/workflow/document/display/' + id,{responseType: 'blob' as 'json'});
}

从Spring引导后端:

我的控制器文件

@GetMapping(value = "/document/display/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody byte[] displayFile(HttpServletResponse response,@PathVariable Integer id) throws Exception {
    return workflowService.getDocument(response,id);
}

我的服务文件

public byte[]  getDocument (HttpServletResponse response,int idaiworkflow) throws Exception{
    
    response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    InputStream inputStream = null;
    try {
        File file = new File(getWorkflowRepo().findByIdaiworkflow(idaiworkflow).getFilename());
        logger.info(file.getAbsolutePath());
        inputStream = new FileInputStream(file);

    } catch (Exception ure) {
        logger.error("WorkflowService - Error getDocument method: " + ExceptionUtils.getStackTrace(ure));
    }
    
    return IoUtils.toByteArray(inputStream);
}

我使用需要Blob来显示文件的查看器组件。 在角度组件中,如果我将{type:'application / msword'}更改为{type:'application / pdf'},则它对于pdf文档工作正常,因此我认为该过程还可以。但这不适用于doc / docx文档,因此我认为{type:'application / msword'}不是唯一要更改的

我该如何解决

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。