如何使用 axios 将 docx 字节转换为文件 Node Js
How to convert docx bytes to file Node Js with axios
我正在从一个端点检索一个 docx 文件并尝试从中创建一个文件。我得到一个文件,但它不可读。可能是我收到文件的格式有问题。
这是我检索信息并创建文件的方式:
const response = await axios.post(
`${process.env.TEMPLATE_HOST}template/data/export`,
{dataId, clientId},
)
fs.writeFileSync(F.path.public(rowFiltered[0]["Files(s) Name"]), response.data);
这是我收到的部分数据
��_rels/.relsP9��Ruڽ�w
��"[Content_Types].xmlP9��R����>~ ��:docProps/core.xmlP9��R��?;��word/theme/theme1.xmlP9��R!��k� ��
docProps/app.xmlP9��R��HR� ���
word/fontTable.xmlP9��R�����
���word/settings.xmlP9���֏� ��_word/webSettings.xmlP9��R��qK��9 ��Sword/styles.xmlP9��R���XI� ���word/document.xmlP9��R
�wi� ���qword/numbering.xmlP9��R�тsf ���vword/_rels/document.xml.rels9��R�тsf�0xword/_rels/header1.xml.relsP9��R�тsf��yword/_rels/footer1.xml.relsP9��R�тsf��{word/_rels/header2.xml.relsP9��R�тsf�=}word/_rels/footer2.xml.relsP9��R�тsf��~word/_rels/header3.xml.relsP9��R�тsf���word/_rels/footer3.xml.relsP9��R����G$�% ��J�word/media/image1.jpegP9��R��CX�d ��զword/endnotes.xmlP9��Ru�/s�j ����word/footnotes.xmlP9��R���?� ����word/header1.xmlP9��R��
]�� ���word/header2.xmlP9��R���?� ���word/header3.xmlP9��R��>�>� ��g�word/footer1.xmlP9��R�k�^�t ���word/footer2.xmlP9��R��>�>� ���word/footer3.xmlPKj�
我已经尝试使用 php 来完成它并且它正确地创建了文件。 php文件大小是47KB,nodejs是83KB,但是比较了部分信息,看起来是一样的。
$fp = fopen('data.docx', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $templateURl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
echo strlen($server_output);
fwrite($fp, $server_output);
fclose($fp);
感谢任何帮助。
不确定,但这可能是因为需要在 axios 上设置一些选项。
也许试试:
const response = await axios.request(
responseType: 'stream',
url: `${process.env.TEMPLATE_HOST}template/data/export`,
method: 'post',
data: {dataId, clientId},
)
fs.writeFileSync(F.path.public(rowFiltered[0]["Files(s) Name"]), response.data);
我正在从一个端点检索一个 docx 文件并尝试从中创建一个文件。我得到一个文件,但它不可读。可能是我收到文件的格式有问题。
这是我检索信息并创建文件的方式:
const response = await axios.post(
`${process.env.TEMPLATE_HOST}template/data/export`,
{dataId, clientId},
)
fs.writeFileSync(F.path.public(rowFiltered[0]["Files(s) Name"]), response.data);
这是我收到的部分数据
��_rels/.relsP9��Ruڽ�w
��"[Content_Types].xmlP9��R����>~ ��:docProps/core.xmlP9��R��?;��word/theme/theme1.xmlP9��R!��k� ��
docProps/app.xmlP9��R��HR� ���
word/fontTable.xmlP9��R�����
���word/settings.xmlP9���֏� ��_word/webSettings.xmlP9��R��qK��9 ��Sword/styles.xmlP9��R���XI� ���word/document.xmlP9��R
�wi� ���qword/numbering.xmlP9��R�тsf ���vword/_rels/document.xml.rels9��R�тsf�0xword/_rels/header1.xml.relsP9��R�тsf��yword/_rels/footer1.xml.relsP9��R�тsf��{word/_rels/header2.xml.relsP9��R�тsf�=}word/_rels/footer2.xml.relsP9��R�тsf��~word/_rels/header3.xml.relsP9��R�тsf���word/_rels/footer3.xml.relsP9��R����G$�% ��J�word/media/image1.jpegP9��R��CX�d ��զword/endnotes.xmlP9��Ru�/s�j ����word/footnotes.xmlP9��R���?� ����word/header1.xmlP9��R��
]�� ���word/header2.xmlP9��R���?� ���word/header3.xmlP9��R��>�>� ��g�word/footer1.xmlP9��R�k�^�t ���word/footer2.xmlP9��R��>�>� ���word/footer3.xmlPKj�
我已经尝试使用 php 来完成它并且它正确地创建了文件。 php文件大小是47KB,nodejs是83KB,但是比较了部分信息,看起来是一样的。
$fp = fopen('data.docx', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $templateURl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
echo strlen($server_output);
fwrite($fp, $server_output);
fclose($fp);
感谢任何帮助。
不确定,但这可能是因为需要在 axios 上设置一些选项。
也许试试:
const response = await axios.request(
responseType: 'stream',
url: `${process.env.TEMPLATE_HOST}template/data/export`,
method: 'post',
data: {dataId, clientId},
)
fs.writeFileSync(F.path.public(rowFiltered[0]["Files(s) Name"]), response.data);