首页 > 解决方案 > 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

标签: angularspring-bootgoogle-chromeblob

解决方案


推荐阅读