Skip to content

Commit fa97f99

Browse files
committed
Upload Multiple Images
1 parent 888d4ea commit fa97f99

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

demo/src/app/tab1/tab1.page.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
</ion-header>
1515

1616
<ion-content>
17-
<ion-button expand="full" (click)="onPickImage()">Add image</ion-button>
17+
<ion-button expand="full" (click)="onPickImage()">Add images</ion-button>
1818
<ion-card *ngFor="let img of images | keyvalue">
1919
<ion-img [src]="img.value" (click)="onClickImage(img.key)"></ion-img>
2020
<ion-progress-bar
2121
*ngIf="uploadStates.has(img.key)"
2222
[color]="uploadStates.get(img.key).color"
2323
[value]="uploadStates.get(img.key).progress"></ion-progress-bar>
2424
</ion-card>
25+
<ion-button expand="full" (click)="onTapUploadButton()">Upload images</ion-button>
2526
</ion-content>
2627
</ion-content>

demo/src/app/tab1/tab1.page.ts

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ export class Tab1Page {
8484
return;
8585
}
8686

87+
const options = {
88+
maximumImagesCount: 100
89+
};
90+
8791
try {
88-
const uris: Array<string> = await this.imagePicker.getPictures({});
92+
const uris: Array<string> = await this.imagePicker.getPictures(options);
8993
const generatedKeys = this.generateUniqueIds(uris.length);
9094
console.log(uris);
9195
uris.forEach((uri, i) => {
@@ -140,19 +144,46 @@ export class Tab1Page {
140144
}
141145
}
142146

143-
uploadImage(id: number) {
144-
const uri = this.imageUris.get(id);
145-
console.log('Upload id', id);
147+
onTapUploadButton() {
148+
for (const id of this.uploadStates.keys()) {
149+
if (!this.uploadStates.has(id)) {
150+
// Start upload
151+
this.uploadImage(id);
152+
} else {
153+
// Remove download
154+
const state = this.uploadStates.get(id);
155+
this.uploader.removeUpload(id, (res) => {
156+
console.log('Remove result:', res);
157+
this.zone.run(() => {
158+
state.status = UploadStatus.Aborted;
159+
state.progress = 1.0;
160+
});
161+
}, async (err) => {
162+
console.warn('Remove error:', err);
163+
const alert = await this.alertController.create({
164+
header: 'Error removing upload',
165+
});
166+
await alert.present();
167+
});
168+
}
169+
}
170+
}
146171

147-
const options = {
148-
serverUrl: 'https://zfir.ngrok.io',
149-
filePath: uri,
150-
fileKey: 'file',
151-
id,
152-
notificationTitle: 'Uploading image'
153-
};
154-
this.uploader.startUpload(options);
155-
console.log('Upload submitted');
172+
uploadImage(id: number) {
173+
for (let i = 0; i < 10; i++) {
174+
const uri = this.imageUris.get(id);
175+
console.log('Upload id', id);
176+
177+
const options = {
178+
serverUrl: 'https://zfir.ngrok.io',
179+
filePath: uri,
180+
fileKey: 'file',
181+
id,
182+
notificationTitle: 'Uploading image'
183+
};
184+
this.uploader.startUpload(options);
185+
console.log('Upload submitted');
186+
}
156187
}
157188

158189
generateUniqueIds(count: number): Array<number> {

0 commit comments

Comments
 (0)