给定一个图像的本地相对url,我怎样才能得到实际的图像发送

我写了一个快速的应用程序,其中一个端点接收图像,然后我将该图像发送到第三方Api。 我用multer将图像保存到磁盘,我有相对的文件path的图像,但我试图调用的API需要实际的图像或图像的url。 问题是,我只传递一个包含文件path的图像的string值。 这是我目前的:

 var multer = require('multer') var storage = multer.diskStorage({ destination: function(req, file, cb) { cb(null, './uploads/'); }, filename: function(req, file, cb) { cb(null, Date.now() + file.originalname); } }); var upload = multer({ storage: storage }); 

我需要在这里调用这个API的图像:

 router.post('/images/tags/info/image', upload.single('fileName'), function(req, res, next) { console.log(req.file.path); var imageUrl = req.file.path; app.models.predict('APP_KEY', imageUrl).then( function (response) { var responseJson = JSON.stringify(response.data.outputs[0].data.concepts); var data = collectTags(responseJson); data.then(function(value) { res.json(value); }); }, function (err) { console.log(err); }); }); 

当我console.log(req.file.filePath) ,我得到一个有效的文件path,但我需要实际的图像传递到app.models.predict(API_KEY, image)

您可以从文件系统读取图像并将其发送到api。
例如

  const bitmap = await q.nfcall(fs.readFile,req.file.path); //remove the temp image fs.unlink(req.file.path); const buffer = new Buffer(bitmap).toString('base64'); app.models.predict('APP_KEY', buffer); 

以base64forms发送图像的示例(使用async / await)。