【问题标题】:Nodejs upload images to other server APINodejs 将图片上传到其他服务器 API
【发布时间】:2017-11-18 00:59:22
【问题描述】:

我想知道是否有人可以帮助解决以下问题。

我有一个带有 node/express 的 Api 用于图片上传

服务器1

var upload = multer({ dest: 'uploads/'})
app.post('/api/upload', upload.single('file'), function(req, res, next) {

if (req.file && req.file.path) { 
   //used to have imgur upload
   //imgur.uploadFile(req.file.path)
   //           .then(function (json) {
   //});
}
});

服务器2

我也有用于图片上传的 PHP Api,我已经设置了使用 pictshare 进行自托管。我想通过将上传重定向到 PHP Api 服务器来将文件上传重定向到新 API。

尝试了multermultipartyneedlerequest 和其他各种方法......但不知何故无法弄清楚。

有没有办法将 multer 引导到新的目的地?

文件正在保存在uploads/ 文件夹中,也许更好的是将该文件上传/定向到新服务器并从 server2 返回新的 url?

四处寻找管道上传图片,但运气不佳..

注意:移动应用正在使用 server1 api,因此如果可以从服务器端处理,则不希望发布应用更新。

【问题讨论】:

    标签: php node.js request image-uploading multer


    【解决方案1】:

    要上传文件(图像或其他),您需要先将其放入驱动器中(或在内存中,但我会先处理驱动器外壳)。然后你可以阅读并发送它。

    接收文件的 API 用于接收表单。通常他们希望收到一个附件为file 字段的表单。

    在这里,您可以看到一个使用 Node 的 'superagent' 库将我驱动器中的现有文件上传到外部 API 的工作示例。

    const
      fs = require('fs'),
      agent = require('superagent');
      const stream = fs.createReadStream('path/to/downloaded/file');
     agent.post(`urlOfApi/uploadFileEndpoint`)
      .type('form')
      .attach('file', stream.path);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      相关资源
      最近更新 更多