angular - Chrome 会阻止 blob 对象并显示下载的文件很危险
问题描述
我正在从服务器下载文件,然后在收到响应后以角度下载我正在使用 blob 下载文件。该文件的类型为 tar.gz 但 google chrome 浏览器显示警告,因为“xyz.tar.gz 不经常下载,可能很危险”,有时它还显示“xyz.tar.gz 可能很危险,所以 Chrome 有阻止它'。我想避免在 chrome 中出现此警告,也不想更改 chrome 的任何设置,例如取消选中安全浏览选项
Code in Component.html file
<button class="download-link" (click)="downloadFile('/supportbundleDownload/20190710')">Click here to download the logs.</button>
Code in component.ts file
downloadFile(url:string) {
var sequenceNumber = url.substring(url.lastIndexOf('/') + 1, url.length);
this.SomeService.downloadFile(url).subscribe(response=> {
var fileName = "logs_complete_" + sequenceNumber + ".tar.gz"
var newBlob = new Blob([response], { type: "application/gzip" });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(newBlob, fileName);
}
else {
var a = document.createElement('a');
a.href = URL.createObjectURL(newBlob);
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
})
Code in component.service.ts
downloadFile(url: string): Observable<Blob> {
return this.http.get(url);
}
The file is getting downloaded but chrome is blocking it and showing the warning. The file type is .tar.gz
解决方案
推荐阅读
- c++ - 跟踪向量中的智能指针
- node.js - Node.js v12 更改了控制台输出
- node.js - 如何从邮递员发送多部分/表单数据
- r - 如何过滤R中的列表
- git - 即使我使用新提交删除了这些文件,也无法使用列入黑名单的模式推送到分支
- web-scraping - Scrapy:满足条件时如何抓取start_urls中的下一个url
- css - 在 created: 中,我的方法不能完美运行
- python - How to convert POST request to GET request in flask?
- docker - ASP.Net 核心 docker 应用程序抛出 socketException
- android - Android上的Libgdx致命异常