简体   繁体   English

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

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

I am using Spring Boot and Angular 4. I have uploaded an image to the project location.我正在使用 Spring Boot 和 Angular 4。我已将图像上传到项目位置。 When I try to view the uploaded image, it is not displayed but an error is thrown.当我尝试查看上传的图像时,它没有显示,但会引发错误。 How can I view the image?如何查看图像?

ang.html: 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>

components.ts:组件.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');
    };
  }

service.ts服务.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 Controller: 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());
    }

在此处输入图片说明

Try updating your mapping in spring boot controller to:尝试将Spring Boot 控制器中的映射更新为:

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

this will indicate that the returned object must be handled as a JPEG image.这将表明返回的对象必须作为 JPEG 图像处理。

In Angular create the required path to image and pass is to the img element, eg:在 Angular 中创建所需的图像路径并传递给 img 元素,例如:

components.ts:组件.ts:

fileUrl = '/getUploadfiles/1';

ang.html: ang.html:

<img [src]="fileUrl">

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

相关问题 如何在Spring MVC中显示存储在数据库中的图像 - How display images that stored in database in spring mvc 无法在 Angular + Spring Boot 项目中使用 *ngFor 显示元素列表 - Unable to display list of elements using *ngFor in Angular + Spring Boot project 如何使用hibernate在spring boot中调用MySQL存储过程? - how to call MySQL stored procedure in spring boot using hibernate? 如何使用 Spring Boot 显示从数据库接收的 blob 图像 - How to display an blob image received from database using Spring Boot 如何在 Spring 启动时使用 ResponseStatusException 将错误消息显示到 Json 中? - How to display error message into a Json using ResponseStatusException on Spring boot? 如何使用 Angular 2 在 Spring Boot 中导出 excel 文件? - How to export a excel file in Spring boot using Angular 2? 如何将数据从 Angular 10 表单传递到包含字符串和文件/图像的 Spring Boot Rest API? - How to Pass Data from Angular 10 form to Spring Boot Rest API that includes both Strings and Files/Images? Spring Boot JPA(Hibernate)如何保存图像 - How Spring Boot JPA(Hibernate) saves Images 使用 spring boot 显示的动态列数 - Dynamic number of columns to display using spring boot 在Spring Boot Rest API中将图像发送到Angular客户端? - Sending images to angular client in spring boot rest api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM