当我尝试在较慢的网络中上传文件时,我在 xcode 中收到以下错误
上传代码:
uploadPhotos(): void {
let message;
this.photos = this.service.getPhotosToUpload();
if (this.photos !== null && this.photos.length > 0) {
const photosList$ = new Array<Observable<boolean[]>>;
this.photos.forEach(photo=> {
const observable = this.submitPhoto(photo)
if (observable) {
photosList$.push(observable);
}
});
zip(photosList$)
.pipe(
switchMap((flags: boolean[][], index: number) => {
let totalNotuploaded = 0;
let totalUploaded = 0;
const results =[]
flags.forEach((flag) => {
totalNotuploaded += flag.filter(falses => !falses).length;
totalUploaded += flag.filter(trues => trues).length;
});
results.push([totalNotuploaded, totalUploaded])
return of(results);
})
).subscribe((results: number[][]) => {
let notuploaded = results[0][0] ;
let uploaded = results[0][1];
this.message = `Uploaded ${uploaded} Photo(s).`;
if (notuploaded > 0) {
this.message += `Errors uploading ${notuploaded}`
}
});
} else {
this.message = `Uploaded 0 Photos.`;
}
}
submitPhoto(photo: PhotoWrapper): Observable<boolean[]> {
const photosToUpload$ = new Array<Observable<boolean>>();
const photosToUpload = photo.filter((filter) => !filter.Uploaded);
photosToUpload.forEach(photo => {
photo.FileContent = this.servic.getFileAsBase64String(new Path(photo.File.directory as any, photo.File.relativePath));
const observable = this.service.upload(photo);
photosToUpload$.push(
observable.pipe(
catchError(() => {
return of(null)
}),
switchMap((result: PhotoResult) => {
return of(result && result?.flag ? true : false)
})
));
});
if (photosToUpload$ && photosToUpload$.length) {
return forkJoin(photosToUpload$);
} else { return null; }
}
//upload service
upload(photosData: PhotoWrapper): Observable<PhotoResult> {
let headers = this.authService.getAuthHeader();
let url = this.configurationService.uploadPhotoUrl();
return this.http.post<boolean>(this.configurationService.uploadPhotoUrl(), photoDto, { headers: headers, observe: 'response' }).pipe(
map((result) =>( {flag: result.body, wrapper: photosData} as PhotoResult)),
);
}
}
当连接到强大的网络 LTE 3 或更多条、5G 或 WIFI 时,我可以毫无问题地上传文件。
如何增加超时或如何防止 NSURLErrorDomain Code=-1001“请求超时”。错误请帮忙
使用本机脚本 http 而不是 Angular httpclient 并增加了超时
const httprequestOption = {
url : 'url',
method : 'POST',
headers : {
"Content-Type": "application/json",
"Authorization": `Basic ${encodedCredentials}`
},
content: JSON.stringify(dto),
timeout : 900000 //Increase Default Timeout
} as HttpRequestOptions;
return from(Http.request(httprequestOption).then((response) => {
const result = response.content.toString();
return result;
})