描述
在 Qt WebEngine (PySide6) 环境中,通过 document:open-buffer 消息传递 Base64 编码的 docx 文件数据时,OnlyOffice 编辑器显示错误:
错误码:-85
错误描述:在打开文件时发生了错误。
文件内容对应于以下格式之一:pdf/djvu/xps/oxps,但该文件的扩展名不一致:docx。
复现步骤
使用 PySide6 QWebEngineView 加载 document 项目
从 Python 端通过 QWebEngineView.page().runJavaScript() 发送 document:open-buffer 消息
Python 端将 docx 文件读取为 bytes,转换为 Base64 后传递
编辑器加载后触发 onError 事件,错误码 -85
日志
[Embed API] Base64 first 50 chars: UEsDBAoAAAAAAIdO4kA...
[Embed API] Decoded file header (hex): 50 4b 03 04
[Embed API] ✅ Valid DOCX file detected (PK header)
[Embed API] Decoded Base64 to ArrayBuffer, size: 10216
[converter] Final documentData.bin header (hex): 50 4b 03 04
[converter] ✅ binData is valid DOCX
[OnlyOffice] Creating new editor instance for: New_Document.docx type: docx
[OnlyOffice] onAppReady triggered
[OnlyOffice] binData type in onAppReady: object
[OnlyOffice] onError: [object Object]
[OnlyOffice] onError details: {"target":{"frameOrigin":"http://127.0.0.1:5000"},"data":{"errorCode":-85,"errorDescription":"在打开文件时发生了错误。
文件内容对应于以下格式之一:pdf/djvu/xps/oxps,但该文件的扩展名不一致:docx。"}}
关键观察:
embed-api.ts 正确解码 Base64 为 ArrayBuffer(文件头 50 4b 03 04)
converter.ts 正确传递 binData
onlyoffice-editor.ts 中 binData type in onAppReady: object —— binData 在 onAppReady 中变成了普通对象,而不是 ArrayBuffer
环境信息
项目 版本
OnlyOffice API 7.4.1
Qt WebEngine PySide6 (Qt 6.x)
Python 3.12+
Node.js 20.19.0+
pnpm 11.4.0
相关文件
embed-api.ts (makeArrayBufferFromPayload):
async function makeArrayBufferFromPayload(payload: Record<string, any>): Promise {
if (payload.data && typeof payload.data === 'string') {
const binaryString = atob(payload.data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer; // 返回 ArrayBuffer
}
}
onlyoffice-editor.ts (createEditorInstance):
export function createEditorInstance(config: {
fileName: string;
fileType: string;
binData: any; // 期望 ArrayBuffer
media?: any;
readonly?: boolean;
}): Promise {
// binData 在 onAppReady 中变成了 object,而不是 ArrayBuffer
}
可能的根本原因
1、Qt WebEngine 的 postMessage 序列化机制:Qt WebEngine 在处理 postMessage 时,ArrayBuffer 类型可能被转换为普通对象
2、window.DocsAPI.DocEditor 的 onAppReady 回调中,binData 丢失了类型信息
3、Vite 编译优化导致代码行为改变
期待行为
document:open-buffer 应正确打开 docx 文件
binData 在 onAppReady 中应保持为 ArrayBuffer 类型
描述
在 Qt WebEngine (PySide6) 环境中,通过 document:open-buffer 消息传递 Base64 编码的 docx 文件数据时,OnlyOffice 编辑器显示错误:
错误码:-85
错误描述:在打开文件时发生了错误。
文件内容对应于以下格式之一:pdf/djvu/xps/oxps,但该文件的扩展名不一致:docx。
复现步骤
使用 PySide6 QWebEngineView 加载 document 项目
从 Python 端通过 QWebEngineView.page().runJavaScript() 发送 document:open-buffer 消息
Python 端将 docx 文件读取为 bytes,转换为 Base64 后传递
编辑器加载后触发 onError 事件,错误码 -85
日志
[Embed API] Base64 first 50 chars: UEsDBAoAAAAAAIdO4kA...
[Embed API] Decoded file header (hex): 50 4b 03 04
[Embed API] ✅ Valid DOCX file detected (PK header)
[Embed API] Decoded Base64 to ArrayBuffer, size: 10216
[converter] Final documentData.bin header (hex): 50 4b 03 04
[converter] ✅ binData is valid DOCX
[OnlyOffice] Creating new editor instance for: New_Document.docx type: docx
[OnlyOffice] onAppReady triggered
[OnlyOffice] binData type in onAppReady: object
[OnlyOffice] onError: [object Object]
[OnlyOffice] onError details: {"target":{"frameOrigin":"http://127.0.0.1:5000"},"data":{"errorCode":-85,"errorDescription":"在打开文件时发生了错误。
文件内容对应于以下格式之一:pdf/djvu/xps/oxps,但该文件的扩展名不一致:docx。"}}
关键观察:
embed-api.ts 正确解码 Base64 为 ArrayBuffer(文件头 50 4b 03 04)
converter.ts 正确传递 binData
onlyoffice-editor.ts 中 binData type in onAppReady: object —— binData 在 onAppReady 中变成了普通对象,而不是 ArrayBuffer
环境信息
项目 版本
OnlyOffice API 7.4.1
Qt WebEngine PySide6 (Qt 6.x)
Python 3.12+
Node.js 20.19.0+
pnpm 11.4.0
相关文件
embed-api.ts (makeArrayBufferFromPayload):
async function makeArrayBufferFromPayload(payload: Record<string, any>): Promise {
if (payload.data && typeof payload.data === 'string') {
const binaryString = atob(payload.data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer; // 返回 ArrayBuffer
}
}
onlyoffice-editor.ts (createEditorInstance):
export function createEditorInstance(config: {
fileName: string;
fileType: string;
binData: any; // 期望 ArrayBuffer
media?: any;
readonly?: boolean;
}): Promise {
// binData 在 onAppReady 中变成了 object,而不是 ArrayBuffer
}
可能的根本原因
1、Qt WebEngine 的 postMessage 序列化机制:Qt WebEngine 在处理 postMessage 时,ArrayBuffer 类型可能被转换为普通对象
2、window.DocsAPI.DocEditor 的 onAppReady 回调中,binData 丢失了类型信息
3、Vite 编译优化导致代码行为改变
期待行为
document:open-buffer 应正确打开 docx 文件
binData 在 onAppReady 中应保持为 ArrayBuffer 类型