簡體   English   中英

如何保存來自 multer memory 磁盤的圖像?

[英]How to save image coming from multer memory disk?

我在 nodejs 中使用 multer 來處理 multipart/formdata 請求並獲取請求中的圖像文件,如下所示:

import multer from "multer";

const upload = multer({
  storage: multer.memoryStorage(),
  limits: { fileSize: 1000000000, files: 2 },
});



app.post("/", upload.single("image"), (req, res , next) => {
      const imageFile = req.file

      dbx
        .filesUpload({ path: "/image.png", contents: imageFile })
        .then((response: any) => {
         
        })
        .catch((uploadErr) => {
         
        });
    }
  )

問題是我無法上傳圖像,它給我一個錯誤,它是緩沖區而不是實際圖像。 如何從req.file生成圖像然后上傳而不將其保存在磁盤上?

您可以解碼數據或更改其編碼,但將其轉換為圖像沒有任何意義。

此處dropbox 提供的示例代碼,讀取utf-8中的文件並上傳。

您也可以通過將發布數據中的緩沖區編碼為utf-8然后上傳該緩沖區,同時將其保存在 memory 中並且永遠不必接觸磁盤。

  const imageFile = req.file.buffer.toString('utf-8');

我通過將緩沖區圖像本身發送到保管箱解決了這個問題,如下所示:

app.post(
  "/",
  upload.single("image"), // or upload single for one file only
  (req, res) => {
    const imageFile = req.file && req.file.buffer;
    // console.log(imageFile);

    dbx
      .filesUpload({ path: "/life.png", contents: imageFile })
      .then((response: any) => {
        console.log("SUccess");
      })
      .catch((uploadErr) => {
        console.log("ERRRR");
        console.log(uploadErr);
      });

    res.json({ message: "hello" });
  }
);


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM