天天看點

七牛base64上傳圖檔帶格式

第一步:選擇圖檔

根據選擇圖檔的多少來循環,使用GUID來從命名圖檔名字,然後添加到數組裡面,再把圖檔轉換成base64位來顯示

for (var i = 0; i < 3; i++) {

                    files = e.target.files[i];

                    var a="";

                    function guid() {

                        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {

                            var r = Math.random() * 16 | 0,

                                v = c == 'x' ? r : (r & 0x3 | 0x8);

                                a += v.toString(16);

                        });

                    };

                    guid();

                    console.log(a);

                    _this.listimgs.push(a + '.' + files.type.substring(6));

                    console.log(_this.listimgs);

                    if (!e || !window.FileReader) return // 看支援不支援FileReader

                    let reader = new FileReader()

                    reader.readAsDataURL(files) // 這裡是最關鍵的一步,轉換就在這裡

                    reader.onloadend = function(i) {

                        console.log(i)

                        _this.src = this.result;

                        var arrs = [];

                        _this.listimg.push(_this.src);

                        console.log(_this.src)

                        console.log(_this.listimg);

                    }

                }

第一步:base64編碼

var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    var base64DecodeChars = new Array(

        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,

        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,

        -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,

        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,

        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,

        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

base64encode(str) {

                var out, i, len;

                var c1, c2, c3;

                len = str.length;

                i = 0;

                out = "";

                while (i < len) {

                    c1 = str.charCodeAt(i++) & 0xff;

                    if (i == len) {

                        out += base64EncodeChars.charAt(c1 >> 2);

                        out += base64EncodeChars.charAt((c1 & 0x3) << 4);

                        out += "==";

                        break;

                    }

                    c2 = str.charCodeAt(i++);

                    if (i == len) {

                        out += base64EncodeChars.charAt(c1 >> 2);

                        out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));

                        out += base64EncodeChars.charAt((c2 & 0xF) << 2);

                        out += "=";

                        break;

                    }

                    c3 = str.charCodeAt(i++);

                    out += base64EncodeChars.charAt(c1 >> 2);

                    out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));

                    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));

                    out += base64EncodeChars.charAt(c3 & 0x3F);

                }

                return out;

            },

            utf16to8(str) {

                var out, i, len, c;

                out = "";

                len = str.length;

                for (i = 0; i < len; i++) {

                    c = str.charCodeAt(i);

                    if ((c >= 0x0001) && (c <= 0x007F)) {

                        out += str.charAt(i);

                    } else if (c > 0x07FF) {

                        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));

                        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));

                        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));

                    } else {

                        out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));

                        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));

                    }

                }

                return out;

            },

第三步:上傳到七牛

先把base64截取到圖檔位置,然後通路七牛的url的時候帶上key(key是你上傳到七牛的檔案名字),key得是經過base64編碼,之後請求成功後是帶格式的圖檔路徑(前提是你的key得帶格式)

var pic = that.listimg[i].substring(22);

                        value = that.base64encode(that.utf16to8(that.listimgs[i]))

                        var url = "http://upload-z2.qiniup.com/putb64/-1/key/" + value;

                        var xhr = new XMLHttpRequest();

                        xhr.onreadystatechange = function() {

                            var keyText = xhr.responseText;

                            if (xhr.readyState == 4) {

                                var responseText = JSON.parse(xhr.responseText);

                                var responsetextkey = responseText.key

                                console.log(responseText.key);

                                imgarr.push(responsetextkey);

                                console.log(imgarr);

                                if (imgarr.length == that.listimg.length) {

                                    that.$axios.post('/AgentWeb/PersonalCenter/ApplySub', {

                                            remittanceperson: that.remitter,

                                            remittancedate: that.remittdate,

                                            remittancevoucher: JSON.stringify(imgarr)

                                        })

                                        .then(res => {

                                            console.log(res);

                                        })

                                }

                            }

                        }

                        xhr.open("POST", url, true);

                        xhr.setRequestHeader("Content-Type", "application/octet-stream");

                        xhr.setRequestHeader("Authorization", "UpToken " + that.qutoken);

                        xhr.send(pic);