Chrome浏览器阻止了Blob对象,并显示下载的文件很危险

时间:2019-07-15 14:06:45

标签: angular spring-boot google-chrome blob

我正在从服务器下载文件,然后在收到响应后以角度显示我正在使用Blob来下载文件。该文件的类型为tar.gz,但是谷歌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

0 个答案:

没有答案