我正在尝试使用Angular 6下载文件。我遇到的问题是,当将二进制文件下载到excel文件时,它已损坏。打开Excel时,它提示“文件格式和扩展名不匹配。文件可能已损坏或不安全。”请帮忙。
export class AppComponent implements OnInit {
constructor(private http: HttpClient) {}
downloadExcelFile() {
const url =
"https://myUrl.getmydata";
this.http
.get(url, { responseType: "blob" })
.subscribe(data => this.downLoadFile(data, "application/vnd.ms-excel"));
}
downLoadFile(data: any, type: string) {
var blob = new Blob([data], { type: type });
var url = window.URL.createObjectURL(blob);
var pwa = window.open(url);
if (!pwa || pwa.closed || typeof pwa.closed == "undefined") {
alert("Please disable your Pop-up blocker and try again.");
}
}
}