【发布时间】:2017-05-07 23:00:13
【问题描述】:
我在我的应用程序中使用 hibernate、html、spring mvc 和 angular js。从休眠中,我正在构建将与前端 html 页面绑定的视图模型。对于上传和保存,我没有任何问题。但是,当使用休眠从数据库中检索相同的图像时,我只接收到一个字节数组。如何获取“tab.namePRAttch”中的文件组件(参见下面的代码)。
如何从字节 [] 中获取与我上传的文件相同的文件?
下面给出了字节数组组件映射到视图模型的java代码。我使用 byte[] 作为图像数据类型。
感谢您的帮助。
提前致谢。
Set<NamePageAttchModel> atchList = result.get(i).getNameAttachments();
List<byte []> attbyArr = new ArrayList<byte[]>();
for(NamePageAttchModel m : atchList){
byte [] a = m.getAttachFile();
attbyArr.add(a);
}
viewModel.setNamePRAttch(attbyArr);
$scope.retrieveName = function() {
if (SearchService.getAdvflagNm()) {
$http({
params: {
"mainId": $scope.mainPR.mainInfoId
},
method: 'GET',
url: 'namePage/retrieveNamePge'
})
.then(
function mySuccess(response) {
$scope.showSuggestions = false;
$scope.disOthrSec = false;
if (response.data !== "" && response.data !== undefined) {
$scope.namePageTabs = response.data;
}
SearchService.setAdvflagNm();
if (response.status == 500) {
},
function myError(response) {});
}
};
<div class="col-md-3">
<input type="file" ng-image-model file-model="tab.namePRFile" multiple />
</div>
<div ng-repeat="file in tab.namePRAttch.file track by $index">
<a ng-src="{{file}}" ng-click="openImage(this)">{{file.name}}</a><i ng-click="removePRNameFile(file)" class="btn btn-md fa fa-trash" aria-hidden="true"></i>
</div>
【问题讨论】:
-
要下载二进制数据,请使用
responseType: 'blob'或responseType: 'arraybuffer'。有关详细信息,请参阅MDN: XHR responseType。 -
如果只有 blob 数据,我们可以使用响应类型:'blob'。但是我还有其他几个字段也可以与 blob 数据一起检索。我没有提到那部分。对不起。
-
我个人选择在 JSON 响应中包含二进制数据是使用 base64 encoding。有关将二进制文件发布到 API 的示例,该 API 然后返回 JSON 响应,数据以 base64 编码,请参阅this DEMO on PLNKR。
标签: javascript java angularjs spring hibernate