我正在编写一个处理程序作为AWS Lambda Function,该处理程序应该将内容包装到docx文件中。我在响应头中使用Content-Disposition以实现目标。下面是我到目前为止编写的代码:
//Process Array Buffer and extract the plain content out of it
export const extractContent = async (data: Buffer) => {
return new Promise((resolve,reject) =>
textract.fromBufferWithMime(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",data,{ preserveLineBreaks: true },(err,content) => {
if (err) {
reject(err);
} else {
resolve(content);
}
}
)
);
};
export const handlerName = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
if (event.body === null) {
return ErrorResponse;
}
const result = await class.func(JSON.parse(event.body)); //returns an array buffer object
if (result instanceof Error) {
return ErrorResponse;
}
const content = await extractContent(result.data)
.then(res => res)
.catch(err => err);
const headers = {
"access-control-allow-origin": "*","strict-transport-security": "'max-age=31536000'","access-Control-Expose-Headers": "Content-Disposition","Content-Disposition": attachment; filename=fn.docx; filename*=UTF-8''fn.docx,"Content-Type": "application/vnd.ms-word.document"
};
return {
body: content,headers,statusCode: 200,};
};
现在只能在APIGatewayResponse中返回字符串;因此,我无法返回字节对象|流缓冲区代替。 我可以下载docx文件,但是无法在MS-Word中打开;我总是收到一个详细的错误消息,即文件已损坏或某些部分无效或丢失。
我已经尝试过base64编码的字符串,但仍然无法获得所需的内容。请提出解决方案。
我使用SLS框架在打字稿节点中编写处理程序。