|
|
@@ -222,6 +222,16 @@ export default function WordPreview() {
|
|
|
return mimeTypes[extension || ''] || 'image/jpeg';
|
|
|
};
|
|
|
|
|
|
+ // File转Base64的辅助函数
|
|
|
+ const fileToBase64 = (file: File): Promise<string> => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const reader = new FileReader();
|
|
|
+ reader.onload = () => resolve(reader.result as string);
|
|
|
+ reader.onerror = reject;
|
|
|
+ reader.readAsDataURL(file);
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
// 替换Word字段并插入图片
|
|
|
const replaceFieldsInWord = async (wordFile: File, excelRow: ExcelRow, rowIndex: number): Promise<Blob> => {
|
|
|
try {
|
|
|
@@ -253,16 +263,21 @@ export default function WordPreview() {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- // 处理图片字段
|
|
|
+ // 处理图片字段 - 修复为base64格式
|
|
|
const folderIndex = (rowIndex + 1).toString();
|
|
|
if (imageMappings[folderIndex]) {
|
|
|
- Object.entries(imageMappings[folderIndex]).forEach(([imageName, imageFile]) => {
|
|
|
- processedData[`image:${imageName}`] = {
|
|
|
- data: imageFile,
|
|
|
- width: 200,
|
|
|
- height: 150
|
|
|
- };
|
|
|
- });
|
|
|
+ for (const [imageName, imageFile] of Object.entries(imageMappings[folderIndex])) {
|
|
|
+ try {
|
|
|
+ const base64Data = await fileToBase64(imageFile);
|
|
|
+ processedData[`image:${imageName}`] = {
|
|
|
+ data: base64Data,
|
|
|
+ width: 200,
|
|
|
+ height: 150
|
|
|
+ };
|
|
|
+ } catch (error) {
|
|
|
+ console.error(`转换图片失败: ${imageName}`, error);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
doc.setData(processedData);
|