From adc9abd145c24f2d3e7033bb738e1e8641eaf4cf Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期二, 02 九月 2025 17:30:43 +0800 Subject: [PATCH] 2025.9.2 --- src/utils/doc.js | 44 ++++++++++++++++++++++++++++++++++++++------ 1 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/utils/doc.js b/src/utils/doc.js index 8082330..fb15036 100644 --- a/src/utils/doc.js +++ b/src/utils/doc.js @@ -5,6 +5,33 @@ import FileSaver from 'file-saver'; /** + * 灏哹ase64鏍煎紡鍥剧墖杞崲涓篈rrayBuffer + * @param {string} base64Str - base64鏍煎紡鍥剧墖瀛楃涓诧紙鍙寘鍚玠ata URL鍓嶇紑锛� + * @returns {ArrayBuffer} 杞崲鍚庣殑ArrayBuffer瀵硅薄 + */ +function base64ToArrayBuffer(base64Str) { + // 绉婚櫎data URL鍓嶇紑锛堝鏋滃瓨鍦級 + const base64Content = base64Str.replace(/^data:image\/\w+;base64,/, ''); + + // 澶勭悊URL瀹夊叏鐨刡ase64瀛楃 + const safeBase64 = base64Content.replace(/-/g, '+').replace(/_/g, '/'); + + // 瑙g爜base64瀛楃涓� + const binaryStr = atob(safeBase64); + + // 杞崲涓篣int8Array + const byteLength = binaryStr.length; + const uint8Array = new Uint8Array(byteLength); + + for (let i = 0; i < byteLength; i++) { + uint8Array[i] = binaryStr.charCodeAt(i); + } + + // 杩斿洖ArrayBuffer + return uint8Array.buffer; +} + +/** * 绛夋瘮渚嬬缉鏀惧浘鐗� * 鏍规嵁鍥剧墖鐨勯暱瀹芥瘮杩涜涓嶅悓鏂瑰紡鐨勭缉鏀� * 濡傛灉瀹藉害澶т簬楂樺害锛堟í鎷嶅浘鐗囷級锛屽垯鎸夌収璁惧畾楂樺害绛夋瘮缂╂斁锛� @@ -122,12 +149,17 @@ getImage(tagValue) { // In this case tagValue will be a URL tagValue = "https://docxtemplater.com/puffin.png" return new Promise(function (resolve, reject) { - JSZipUtils.getBinaryContent(tagValue, function (error, content) { - if (error) { - return reject(error); - } - return resolve(content); - }); + if (tagValue.indexOf('http') == 0) { + JSZipUtils.getBinaryContent(tagValue, function (error, content) { + if (error) { + return reject(error); + } + return resolve(content); + }); + } else if (tagValue.indexOf('data:image') == 0) { + const buffer = base64ToArrayBuffer(tagValue); + return resolve(buffer); + } }); }, -- Gitblit v1.9.3