最后,我首先从xhrequest的响应创建Blob,然后将其转换为文件对象。
发件人.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class SenderService {
postUrl = 'http://localhost:5000/post_url';
fetchAudioUrl = 'http://localhost:4200/assets/';
constructor(private http: HttpClient) {}
send (audioFile: File) {
const formData: FormData = new FormData();
formData.append('file', audioFile, audioFile.name);
return this.http.post(this.postUrl, formData);
}
getAudio (fileName: string) {
return this.http.get(this.fetchAudioUrl + fileName, { responseType: 'blob'});
}
}
演示.component.ts
import { SenderService } from './sender.service';
@Component({
......
providers: [SenderService]
})
export class DemoComponent {
constructor(private senderService: SenderService) {}
buttonClicked(req: any): void {
this.senderService.getAudio(req.fileName)
.subscribe((data: any) => {
const file = new File([data], req.fileName);
this.senderService.send(file)
.subscribe((data: any) => {
console.log(data);
}, error => {
console.log(error);
});
});
}