NextJS部署AWS Amplify后生成的DOCX文件损坏求助
AWS Amplify部署NextJS应用后生成的Word文档损坏问题
现象
- 本地环境:通过docx.js生成Word文档,使用file-saver的
saveAs方法下载,文档生成、下载、打开流程均正常 - Amplify部署后:下载的文档打开时弹出错误提示:
Word experienced an error trying to open the file. Try these suggestions:
- Check the file permissions for the document or drive
- Make sure there is sufficient free memory and disk space
- Open the file with the Text Recovery convertor
- 补充:下载的文档右键点击→属性→勾选「解除锁定」选项后,可正常打开
客户端核心代码
点击事件处理函数
const handleGenerateBORDocument = async () => { setLoading(true); handleCloseCreateMenu(); try { const { blob, filename } = await generateBORWordDoc(company); if (blob) { saveAs(blob, filename); } if (company.status === "Ready To File") { await updateCompanyStatus(company._id, "Awaiting Filing"); location.reload(); } } catch (err) { console.error("Error generating document:", err); alert("Failed to generate document"); } finally { setLoading(false); } };
Word文档生成函数
export async function generateBORWordDoc(company) { const today = new Date(); const bORRows = handleCreateBORPages(company.beneficialOwners); const doc = createCompanyDoc(company, today, bORRows); const blob = await Packer.toBlob(doc); const filename = `${company.name || "Company"} - ${today.toISOString().slice(0, 10)}.docx`; return { blob, filename }; }
已尝试的方案
参考建议修改Amplify项目的customHttp.yml文件,添加MIME类型配置:
customHeaders: - pattern: "**/*.docx" headers: - key: Content-Type value: application/vnd.openxmlformats-officedocument.wordprocessingml.document - pattern: "**/*.zip" headers: - key: Content-Type value: application/zip
疑问
为何本地环境正常的Word文档,部署到Amplify后会出现损坏(需解除锁定才能打开)的情况?
内容的提问来源于stack exchange,提问作者Rich
相关产品推荐
相关产品推荐

