首页 > 解决方案 > 如何在反应中从服务器获取文件

问题描述

我的控制器中有方法:

    @GetMapping("/photos")
    public ResponseEntity<?> getPhotos(){
        List<Photo> photoList = photoService.findAll();
        return new ResponseEntity<>(photoList, HttpStatus.OK);
    }

它从数据库中返回数组: [{"id": 1,"name": "photo1.jpg","file": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wC...(byte[])"},{...},...] 我知道一个文件是这样完成的:

.then(response => {
   response.blob().then(blob => {
      let url=  window.URL.createObjectURL(blob);
      let a = document.createElement('a');
      a.href = url;
});
});

但是如何处理数组?

标签: javareactjsspringdownloadblob

解决方案


您可以改用 response.json()。

.then(response => {
    response.json().then(responseJSON => {
        // responseJson should be an array that you can iterate on and manipulate individual objects and parse relevant keys. 
    });
});

推荐阅读