在ExpressJS中添加参数到文件path

我正在尝试上传特定照片,以引用公共/资产目录中的客户端文件夹。 文件path应该是public / assets /:id。 但是,从我的代码,当它运行的文件path总是返回公共/资产/未定义。 有没有人有如何解决这个问题的build议? 这里是快递的代码。

app.param('id', function (req, res, next, id) { console.log(req.params.id); next(); }); app.post('/file-upload', function(req, res) { // get the temporary location of the file var tmp_path = req.files.thumbnail.path; // set where the file should actually exists - in this case it is in the "images" directory var target_path = './public/assets/'+req.params.id; // move the file from the temporary location to the intended location fs.rename(tmp_path, target_path, function(err) { if (err) throw err; // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files fs.unlink(tmp_path, function() { if (err) throw err; res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes'); }); }); }); 

在路由中,你没有传递你的参数,请检查并改变你的路由逻辑

 app.post('/file-upload/:id', function(req, res) { var target_path = './public/assets/'+req.params.id; //or use req.param('id') ................ }); 

通过你的第二段传递链接是你的id示例: http:// localhost:port / file-upload / 123

如果遇到问题,请使用'?'作为查询string传递variables。 操作者

  app.post('/file-upload', function(req, res) { var target_path = './public/assets/'+req.query.id; ................ }); 

发布链接就像这个例子: http:// localhost:port / file-upload?id = 123

您可以在.post的第一个参数中使用:id

 app.post('/file-upload/:id', function(req, res) { 

然后像req.params.id一样在代码中进行req.params.id

在HTML中,您应该根据要传递的id更改actionpath。