繁体   English   中英

如何使用 Angular 4 在 Spring Boot 中显示存储的图像?

[英]How to display stored images in Spring Boot using Angular 4?

我正在使用 Spring Boot 和 Angular 4。我已将图像上传到项目位置。 当我尝试查看上传的图像时,它没有显示,但会引发错误。 如何查看图像?

ang.html:

<button type="button" class="button btn-info" 
                            (click)='showInfo()'>ShowImages</button>
                <div class="panel panel-primary">
                    <div class="panel-heading">List of Images</div>
                    <img [src]="fileUploads">
                </div>

组件.ts:

fileUploads: any;
  sid: number = 1;
  showInfo() {   
    this.drugservice.getFiles(this.sid).subscribe(data => {this.fileUploads = data, alert(data)}), err => {
      console.log('Error Occured showInfo');
    };
  }

服务.ts

getFiles(id: number): any {
  return this.http.get(this.getFileuploadedURL+'/'+id, { responseType: ResponseContentType.Blob }).map(
  (res) => {
      return new Blob([res.blob()], { type: 'image/png' })
  });
}

springBoot 控制器:

@GetMapping(value = "/getUploadfiles/{id}")
    @ResponseBody
    public byte[] getImage(@PathVariable Integer id) throws IOException {
        String filename = "01";
        System.out.println("id : " + id);
        File serverFile = new File(rootLocation + "\\" + filename + ".jpg");
        System.out.println("serverFile : " + serverFile);        
        return Files.readAllBytes(serverFile.toPath());
    }

在此处输入图片说明

尝试将Spring Boot 控制器中的映射更新为:

@GetMapping(value = "/getUploadfiles/{id}", produces = MediaType.IMAGE_JPEG_VALUE)

这将表明返回的对象必须作为 JPEG 图像处理。

在 Angular 中创建所需的图像路径并传递给 img 元素,例如:

组件.ts:

fileUrl = '/getUploadfiles/1';

ang.html:

<img [src]="fileUrl">

暂无
暂无

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

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