vuejs如何将线上PDF转为base64编码

只需要两个方法-下载与转换:

下载方法:

            demoDownloadPDF(url) {
                // if (!(/^https?:/i.test(url))) return;
                if (window.XMLHttpRequest) var xhr = new XMLHttpRequest(); else var xhr = new ActiveXObject("MSXML2.XMLHTTP");
                xhr.open('GET', url, false); //同步方式
                if (xhr.overrideMimeType)
                    try {
                        xhr.responseType = 'arraybuffer';
                        var arrybuffer = true;
                    } catch (err) {
                        xhr.overrideMimeType('text/plain; charset=x-user-defined');
                    }
                xhr.send(null);
                var data = xhr.response || xhr.responseBody;
                if (typeof Uint8Array !== 'undefined') {
                    if (arrybuffer) var dataArray = new Uint8Array(data); else {
                        var dataArray = new Uint8Array(data.length);
                        for (var i = 0; i < dataArray.length; i++) { dataArray[i] = data.charCodeAt(i); }
                    }
                } else
                    var dataArray = VBS_BinaryToArray(data).toArray(); //兼容IE低版本
                return this.demoGetBASE64(dataArray);
            },

下载后将pdf转换: 

            demoGetBASE64(dataArray) {
                var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
                var strData = "";
                for (var i = 0, ii = dataArray.length; i < ii; i += 3) {
                    if (isNaN(dataArray[i])) break;
                    var b1 = dataArray[i] & 0xFF, b2 = dataArray[i + 1] & 0xFF, b3 = dataArray[i + 2] & 0xFF;
                    var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
                    var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
                    var d4 = i + 2 < ii ? (b3 & 0x3F) : 64;
                    strData += digits.substring(d1, d1 + 1) + digits.substring(d2, d2 + 1) + digits.substring(d3, d3 + 1) + digits.substring(d4, d4 + 1);
                }
                return strData;
            },

你可能感兴趣的:(常用方法,js,pdf,前端,javascript,vue.js)