首页 > 解决方案 > 如何以角度连接 forkJoin() 返回的所有结果?

问题描述

我试图从 forkJoin() 返回的数组中获取单个字符串。我有一个大文件,因此我将其分成块并调用 API 和 forkJoining 响应。但我看到我得到了大约 40 个数组。这不是我所期待的。如何连接所有结果数据以表示单个文件数据?

这是我的代码:

比较服务.ts

getFileToCompare (fileSize: number, userRole: String, inFileName: String, inFilePath: String): Observable<any> {
let chunks = [{}];
const chunkSize = 1024 *1024* 20; // 20 kB each chunk
let j = 0;
for (let index = 0; index < fileSize; index += chunkSize + 1) {
chunks[j++] = { startRange: index, endRange: index + chunkSize > fileSize ? fileSize : index + chunkSize }; 
}    
let headers = [];
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
const chunk = chunks[chunkIndex];
const httpHeaders = new HttpHeaders().append('startrange', chunk['startRange']).append('endrange', chunk['endRange']);

headers.push(this.httpClient.post(serviceUri + '/api/getFileToCompare', {
userrole: userRole,
srcfileName: inFileName,
srcfilePath: inFilePath
}, { headers: httpHeaders, responseType: 'json' }));
}      
return forkJoin(headers);
}

我的 API:

var options = {
rangeStart: startRange,
rangeEnd: endRange
};
blobService.getBlobToText(containerName, fileName, options, (err, data) => {
if (err) {
log.error(err);
res.status(200).json({
status: 'failed',
data: err
});
} else {
res.status(200).json({
status: 'success',
data: data
});
}
});

比较.component.ts

this.jobService.getFileToCompare(fileSize, this.user.roles[0],  fileName,path).toPromise()
.then((res) => {
console.log(res)// n no.of array   ...[{status:'success',data:'hdghdh'}{status:'success',data:'xxx'}]
this.originalModel = Object.assign({}, this.originalModel, { code:res });  // res should a string 'hdghdhxxx'
this.modifiedModel = Object.assign({}, this.originalModel, { code:res });
});
}

标签: node.jsangulartypescriptazure

解决方案


推荐阅读