java - 如何在反应中从服务器获取文件
问题描述
我的控制器中有方法:
@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;
});
});
但是如何处理数组?
解决方案
您可以改用 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.
});
});
推荐阅读
- ios - 快速使用 UIDocumentPickerViewController(documentTypes:)
- python - 数组中复数的大小
- azure - Azure Blob 更改源缺少 blob 追加事件
- ios - 在 UITableView 中默认启用重新排序控件而不处于编辑模式
- git - 如何获取添加为 git 子模块的依赖项的子依赖项
- html - 当第二个操作数具有“隐藏”属性时应用兄弟选择器
- spring-webflux - Spring webflux restdocs - 通过测试用例的安全性
- html - 如何在第一个对象之后和按钮对象之前正确附加?
- python - Python 和 MySQL 中的数据透视表
- javascript - 点击不打开文件对话框