类型 'Observable<void>' 不可分配给类型 'Observable<HttpEvent<any>>' 类型 'void' 不可分配给类型 'HttpEvent<any>'
Type 'Observable<void>' is not assignable to type 'Observable<HttpEvent<any>>' Type 'void' is not assignable to type 'HttpEvent<any>'
我正在尝试为上传添加进度条,但出现此错误。
并且 event.type 未定义。
请帮我找到解决办法。谢谢
我附上了我完成的代码。
HttpRequest代码
uploadfile(file: any): Observable<HttpEvent<any>>{
return this.InfoService.getId().pipe(
concatMap((result) => {
return this.schemaService.getSchema().pipe(schema => {
const url = '/url';
return this.http.post(url, file, {headers: this.headers, reportProgress: true});
}).toPromise().then((resolved) => {
this.successMessage('Upload Success',MessageIcon, " uploaded successfully");
})
}));
}
订阅方式:
uploadfile(){
const formData = new FormData();
formData.append('file', this.uploadForm.get('item').value);
this.uploadService.uploadfile(formData).subscribe((event : HttpEvent<any>) => {
console.log(event)
switch (event.type) {
case HttpEventType.UploadProgress:
this.progress = Math.round(event.loaded / event.total * 100);
console.log(`Uploaded! ${this.progress}%`);
break;
case HttpEventType.Response:
console.log('successfull!', event.body);
setTimeout(() => {
this.progress = 0;
}, 1500);
}
})
}
我不确定您要对 pipe
函数的 schema
参数做什么。 pipe
将 RxJS 运算符作为参数。从用法来看,我假设你想在这里使用像 concatMap
这样的映射运算符。
函数的return类型声明为Observable<HttpEvent<any>>
。但是您正在使用 toPromise()
函数将 Observable 转换为 Promise。您可以改用 tap
运算符。
尝试以下方法
uploadfile(file: any): Observable<HttpEvent<any>> {
return this.InfoService.getId().pipe(
concatMap((result) => this.schemaService.getSchema()),
concatMap((schema) => {
const url = '/url';
return this.http.post(url, file, {
headers: this.headers,
reportProgress: true,
observe: "events" // <-- also try adding `observe`
});
}),
tap(() => this.successMessage('Upload Success', MessageIcon, " uploaded successfully"))
);
}
我正在尝试为上传添加进度条,但出现此错误。 并且 event.type 未定义。
请帮我找到解决办法。谢谢
我附上了我完成的代码。
HttpRequest代码
uploadfile(file: any): Observable<HttpEvent<any>>{
return this.InfoService.getId().pipe(
concatMap((result) => {
return this.schemaService.getSchema().pipe(schema => {
const url = '/url';
return this.http.post(url, file, {headers: this.headers, reportProgress: true});
}).toPromise().then((resolved) => {
this.successMessage('Upload Success',MessageIcon, " uploaded successfully");
})
}));
}
订阅方式:
uploadfile(){
const formData = new FormData();
formData.append('file', this.uploadForm.get('item').value);
this.uploadService.uploadfile(formData).subscribe((event : HttpEvent<any>) => {
console.log(event)
switch (event.type) {
case HttpEventType.UploadProgress:
this.progress = Math.round(event.loaded / event.total * 100);
console.log(`Uploaded! ${this.progress}%`);
break;
case HttpEventType.Response:
console.log('successfull!', event.body);
setTimeout(() => {
this.progress = 0;
}, 1500);
}
})
}
我不确定您要对
pipe
函数的schema
参数做什么。pipe
将 RxJS 运算符作为参数。从用法来看,我假设你想在这里使用像concatMap
这样的映射运算符。函数的return类型声明为
Observable<HttpEvent<any>>
。但是您正在使用toPromise()
函数将 Observable 转换为 Promise。您可以改用tap
运算符。
尝试以下方法
uploadfile(file: any): Observable<HttpEvent<any>> {
return this.InfoService.getId().pipe(
concatMap((result) => this.schemaService.getSchema()),
concatMap((schema) => {
const url = '/url';
return this.http.post(url, file, {
headers: this.headers,
reportProgress: true,
observe: "events" // <-- also try adding `observe`
});
}),
tap(() => this.successMessage('Upload Success', MessageIcon, " uploaded successfully"))
);
}