使用 Multer(使用 Express)访问嵌套数据

问题描述 投票:0回答:0

如何访问属于对象数组的所有已上传图像并将它们保存到 /uploads?

我的请求体看起来像这样:

{
  client: 'my_client_test',
  city: 'my_city_test',
  spots: [
    {
      photo_1: 'C:\\fakepath\\photo_1_test.jpeg',
      photo_2: 'C:\\fakepath\\photo_2_test.jpeg',
      address: 'my_address_test'
    }
  ]
}

我需要保存点数组的所有 photo_1 和 photo_2。

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "uploads/");
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    },
});

const upload = multer({ storage });

app.post("/admin/add", /* HERE */, (req, res) => {
    console.log(req.files);   // undefined
    console.log(req.body);
});

这里是输入:

<div class="spot">
        <label for="photo_1_0">Photo 1:</label>
        <input type="file" id="photo_1_0" name="photo_1_0" required />
        <label for="photo_2_0">Photo 2:</label>
        <input type="file" id="photo_2_0" name="photo_2_0" required />
        <label for="address_0">Address:</label>
        <input type="text" id="address_0" name="address_0" required />
</div>

输入被命名为 photo_1_0、photo_2_0 和 address_0,因为用户可以单击一个按钮并添加另一个地点(photo_1_1、photo_2_1、address_1)

javascript node.js express multer
© www.soinside.com 2019 - 2024. All rights reserved.