我有一個用於上傳圖片的服務。要上傳圖片,我所做的只是Angular 2:將幾個http調用合併爲一個
return this.http.post(/* ... */)
而且我收到了我可以在我的組件中訂閱的訂閱。但是,當我要上傳幾張照片,我要做
for (let p of pics) { this.http.post(/* ... */); }
我的問題是,我想回的所有調用的結果,而不只是一個電話。那可能嗎 ?
編輯這裏是我的服務
addPictures(files: File[], folder: string): Observable<Parse.Object[]> {
let hasError = false;
for (let file of files) {
let [type, ext] = file.type.split('/');
if (type.toLowerCase() !== 'image' || !environment.imgExts.includes(ext.toLowerCase())) { hasError = true; }
}
if (hasError) { return Observable.throw('Invalid extension detected'); }
let observables: Observable<Parse.Object>[] = [];
for (let file of files) {
// Get its size
let img = new Image();
img.onload =() => {
// Create the Parse document
let parseImg = { url: '', type: file.type, width: img.width, height: img.height };
// Upload it on Amazon and add it to DB
observables.push(this.addPicture(parseImg, file, folder));
}
img.src = window.URL.createObjectURL(file);
}
return Observable.forkJoin(observables);
}
[如何知道何時所有的Angular2 HTTP調用完成]可能的重複(https://stackoverflow.com/questions/36014508/how-to-know-when-all-angular2-http-calls-are-finished ) – echonax
fyi,我沒有downvote問題 – echonax
我正在看這個答案現在,謝謝 – trichetriche