js base 64 파일 다운로드

1152 단어 js
$scope.base64ToBlob = function(code) {
        var parts = code.split(';base64,');
        var contentType = parts[0].split(':')[1];
        var raw = window.atob(parts[1]);
        var rawLength = raw.length;
        var uInt8Array = new Uint8Array(rawLength);
        for(var i = 0; i < rawLength; ++i) {
            uInt8Array[i] = raw.charCodeAt(i);
        }
        return new Blob([uInt8Array], {
            type: contentType
        });
    };

/**
     *     

     */
    $scope.downloadDocument = function () {
        var code = "data:" + $scope.documentType + ";base64, " + $scope.documentData;
        var aLink = document.createElement('a');
        var blob = $scope.base64ToBlob(code); //new Blob([content]);
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("click", true, true);
        //initEvent         FF          ,    ,            
        aLink.download = "    -" + $filter('date')(new Date(), "yyyy-MM-dd");
        aLink.href = URL.createObjectURL(blob);
        aLink.click();
    }

좋은 웹페이지 즐겨찾기