将blob-data和一个字符串一起发送到后端

发布于 2025-01-31 10:56:29 字数 1009 浏览 4 评论 0原文

我有一个奇怪的问题。 使用节点,react,express,mongodb - > Mern Stack。

因此,我的页面生成了一个PDF文件,然后将其发送到后端(作为BLOB数据)并存储在那里。 我遇到的问题是,现在我需要发送付款ID以及BLOB数据,以将订单保存在数据库中。我在一个帖子请求中都需要两者,以使其尽可能流畅:

 await axios
        .post(process.env.REACT_APP_SERVER_API + '/payment/cash', {
          blobData,
          paymentId
        })
        .then(async (res) => ...

这样。

之前,当我刚刚发送BLOB数据时,我可以通过写作简单地访问后端中的数据:

exports.createCashOrder = async (req, res) => {
  const { filename } = req.file; // THIS RIGHT HERE
  const fileHash = await genFileHash(filename);

  try {
    await saveOrder(filename, fileHash, "cash", paymentId);
    //await sendOrderCreatedEmail(req.body, fileHash);
    //await sendOrderReceivedConfirmEmail(req.body, fileHash);
    res.send({ filename: filename });
  }

但这不再起作用。发送该请求对象时,我无法访问该文件对象。 除了提出两个分开的帖子请求之外,都不尝试

req.body.blobData
req.body.blobData.file
req.file

任何想法如何实现这一目标?

欢呼,很高兴为您提供帮助!

I´ve got a weird problem.
Using Node, React, Express, MongoDB -> MERN Stack.

So my page generates a PDF file which then gets send to the backend (as blob data) and is being stored on there.
The problem I have, now I need to send a payment ID along with that blob data to save the order in the data base. I need both in one post request, to make it as smooth as possible:

 await axios
        .post(process.env.REACT_APP_SERVER_API + '/payment/cash', {
          blobData,
          paymentId
        })
        .then(async (res) => ...

like so.

Before, when I just sent the blob data, I could simply access the data in the backend by writing:

exports.createCashOrder = async (req, res) => {
  const { filename } = req.file; // THIS RIGHT HERE
  const fileHash = await genFileHash(filename);

  try {
    await saveOrder(filename, fileHash, "cash", paymentId);
    //await sendOrderCreatedEmail(req.body, fileHash);
    //await sendOrderReceivedConfirmEmail(req.body, fileHash);
    res.send({ filename: filename });
  }

But that doesn't work anymore. I dont have access to that file object anymore when sending that request object.
Neither by trying

req.body.blobData
req.body.blobData.file
req.file

Any idea how to achieve that, except from making two seperate post requests?

Glad for any help, cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2025-02-07 10:56:29

将数据作为表单发送

 await axios
        .postForm(process.env.REACT_APP_SERVER_API + '/payment/cash', {
          blobData,
          paymentId
        })
        .then(async (res) => ...

,然后使用 multer mifdertare 表达。

Send the data as a form

 await axios
        .postForm(process.env.REACT_APP_SERVER_API + '/payment/cash', {
          blobData,
          paymentId
        })
        .then(async (res) => ...

And then use multer middleware to handle the form in express.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文