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 (将#修改为@)