首页 > 解决方案 > 无法在 Node.js 中使用 Axios 将 Blob 保存到网络目录

问题描述

我有一个想要保存到网络位置的 blob(pdf)。到目前为止,我已经能够保存一个 pdf,但是即使我已经验证 blob 的大小表明它是完整的 PDF,它也是空的。

以下是我所做的一些步骤:

将 blob 发布到后端路由的函数

                    const postPDF = async () => {
                    const doc = docViewer.getDocument();
                    const xfdfString = await annotManager.exportAnnotations();
                    const options = { xfdfString };
                    const data = await doc.getFileData(options);
                    const arr = new Uint8Array(data);
                    const blob = new Blob([arr], { type: 'application/pdf' });

                    const token = state.token
                    const headers = {
                        'x-auth-token': token,
                        "content-type": blob.type
                    }
                    console.log(blob)
                    try {
                        const response = await axios.post(
                            postW4,
                            blob,
                            { headers: headers });
                        console.log(response.data.message)
                    } catch (e) {
                        console.log(e);
                    }
                };

用于发布和保存 BLOB(PDF) 到网络位置的后端代码:

    const handleUpload = (req, res, next) => {
    console.log('got here')
    console.log(__dirname)
    const tmp = fs.writeFileSync(
        path.join(__dirname, './test.pdf'),
        req,
    );
}
    router.post('/w4', async(req, res) => {
    // upload.any(req);
    handleUpload(req)
    // res.send({ sucess: true })
    });

我尝试将其转换为 formData 并尝试实现 multer,但仍然无法保存 pdf。

标签: javascriptnode.jsreactjsaxiosblob

解决方案


推荐阅读