I need a download image function to support multiple browers

时间:2015-07-31 20:27:12

标签: javascript angularjs firefox download safari

I have a function that works beautifully in Chrome. The user checks multiple photos and can download them to their computer. Photos are saved in the download folder. These photos are being pulled from my amazonS3 account.

However, in Safari and Firefox, when the user hits the download button it displays the first image in the array in the browser window. No download happens.

Is there a way to modify my code to support Safari and Firefox as well? I need the photos to save in the Downloads folder on the desktop. Im using AngularJS.

The first function goes through the checked photos and puts them in an array. The second function downloads the photos.

Code below:

$scope.downloadPhotos = function() {
    var photoUrls = [];
    for (var x = 0; x < $scope.$parent.photos.length; x++) {

        var p = $scope.$parent.photos[x];
        if (p.isChecked) {
            photoUrls.push($scope.bucketUrl() + p.photoUrl);

        }
    }

    saveImage(photoUrls);
};

function saveImage(urls) {
    var link = document.createElement('a');

    link.setAttribute('download', null);
    link.style.display = 'none';

    document.body.appendChild(link);

    for (var i = 0; i < urls.length; i++) {
        link.setAttribute('href', urls[i]);
        link.click();
    }

    document.body.removeChild(link);
}

0 个答案:

没有答案