Multer Req.files.path 不可编辑?

Multer Req.files.path not editable?

我目前正在尝试通过 Multer 文件上传更改 req.files.path 中多个文件的路径,但路径似乎不想更改。这是我的代码:

app.post("/api/imageUpload", upload.array("uploadedImages"), function(req, res) {
    req.files.forEach(function(file) {
        file.path.replace(/\/g, "/").substring("public".length);
        console.log(file.path);
    });
    res.status(200).send({files: req.files});
});

我文件的原始路径类似于 public\uploads\filename。

此代码试图将路径中的反斜杠替换为正斜杠。当我 console.log 路径时,没有任何变化。当我 res.send 通过 AJAX 调用将文件传输到浏览器时,没有任何变化(当我尝试使用 .replace 方法更改我的 JS 文件中的路径时,它仍然不起作用) .我还有其他部分代码,其中 Multer 只上传一个文件并且路径可以很好地改变;但是,我正在将路径分配给一个变量,所以这可能就是问题所在。我只想知道这是否是问题所在(如果是,为什么?)

非常感谢任何帮助!

replace() 字符串函数不会改变字符串。如果要替换字符串,请使用:

req.files.forEach(function(file) {
    file.path = file.path.replace(/\/g, "/").substring("public".length);
    console.log(file.path);
});