<script>
JSZipUtils.getBinaryContent('http://static.yygo.tv/Gift/o_1c3239h691nd2uohmf414b8dlia.zip', function (err, data) {
if (err) {
throw err; // or handle err
}
var zip = new JSZip(data);
var imgArr = [],
image = {};
for (var i in zip.files) {
imgArr.push(i)
};
var len = imgArr.length;
for (var j = 1; j < len; j++) {//循環圖片Base64
var fileName = imgArr[j],
buffer = zip.file(imgArr[j]).asArrayBuffer(),
str = _arrayBufferToBase64(buffer),
pIndex = fileName.indexOf('.'),
type = fileName.substr(pIndex + 1),
re = 'data:image/' + type + ';base64,';
document.getElementById("gift").src = re + str;
image[fileName] = re + str;
};
console.log(image)
})
//img to Base64
function _arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
</script>
Tip=>jsZip讀取的data爲對象,以上操做將其轉爲ARRAY形式,並使用BASE64轉碼函數將讀取到的img文件的ARRAYBUFFER數據轉爲BASE64,buffer參數爲每一個圖片的文件名,可經過zip.file('')||zip.folder()讀取文件名或文件夾