【问题标题】:how to get this image and display it in angular 4 using http?如何获取此图像并使用http以角度4显示它?
【发布时间】:2018-06-27 12:46:16
【问题描述】:

这是我的 java spring boot 代码。下面的代码是获取图像的其余函数。使用http get方法如何调用该方法并在角度4上显示图像?

 @RequestMapping(value = "/get-file",method = RequestMethod.GET)
    public void getImage(HttpServletResponse response) throws IOException
    {
        Long a = new Long(6);
        ImageModel imageModel= imageRepository.getOne(a) ;

        InputStream in = new ByteArrayInputStream(imageModel.getPic()); 
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
        //System.out.println("get image ");
    }

这是我的角码

public getCountry()
  {
    console.log("hey");
    let url = 'http://localhost:8080/get-file';
   return this._http.get(url,{ responseType: ResponseContentType.Blob }).map((res)=>res.blob());
  }

【问题讨论】:

  • 到目前为止,您在 Angular 中尝试了什么?

标签: angular spring-boot angular-http angular4-forms


【解决方案1】:
  1. 使用 HTTPCLIENT(如果 angular v4+)从 Angular 调用服务,如果 CORS 问题使用 proxy-config.json 文件,或者启用 cors。

  2. 从角度调用服务中检索响应

  3. 通过响应以角度显示图像

** 使用 CDN + 从后端到前端发送 URL 可能会更好——我的 2cents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2019-03-28
    相关资源
    最近更新 更多