简体   繁体   中英

Download file is not working in latest version of google chrome [65.0.3325.181 (Official Build) (64-bit)]

Below function was working fine in previous verion of chrome 64.0.3282.186 (Official Build) (64-bit) , but same is not working in latest version of google chrome. [65.0.3325.181 (Official Build) (64-bit)] .Insteadof downloading file, its opening in same tab.

Why this change in behaviour came in latest version of chrome and how we can achieve this ?

This is Function having javaScript lines to download file in same tab:

$scope.downloadFile = function (file) {
            var downloadLink = document.getElementById('downloadDocLink'); 
            downloadLink.target = '_self';
            downloadLink.href = file.serverPath;
            downloadLink.download = file.filename;
            downloadLink.click();

        }

HTML :

<a id="downloadDocLink" ng-hide="true"></a>
<button ng-click="downloadFile(file)">Download</button>

Encountered same problem in my project. Download failures are now behave like navigation to different page rather than message in download bar. Since version 65, Chrome has different behavior for download. Google blocked cross-origin Deprecations and removals in Chrome 65

Here's how my download script that I wrote a while back, it works with the latest version of Chrome too, parameters to this function are; blob (blob object to download) and name (string for file name)

if (navigator.msSaveBlob)
    return navigator.msSaveBlob(blob, name);

var a = $("<a style='display: none;'/>");

var url = window.URL.createObjectURL(blob);
a.attr("href", url);
a.attr("download", name);
$("body").append(a);
a[0].click();
window.URL.revokeObjectURL(url);
a.remove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM