图片未使用request.post上传,但未获取值

时间:2020-07-09 15:00:48

标签: node.js express sails.js

const formData = req.file('image')

const options = {
      url: 'myurl'',
      method: 'POST',
      formData : formData
}

request.post(options, async function(err,result,body){
    if(err)
    {
      res.json({
               success: false, 
               message: "There are some error occured in file upload"
              });
    }
    else
    {
      res.json({success: true});
    }
});

我正在尝试使用request.post上传图片,但不允许我上传图片,请指导如何修复

请指导

Edit1:

  1. 我正在使用ejs作为前端。
  2. 尝试将上传的图像从一台服务器(example1.com)发送到另一台服务器(example2.com)。
  3. Server1(example1.com)由sails.js驱动。

谢谢

1 个答案:

答案 0 :(得分:0)

  1. 在ejs表单中使用enctype ='multipart / form-data'。

  2. 由于您是使用microservervice将图像从一个后端(example1.com)发送到另一个后端(example2.com)的,因此请先将该图像保存在第一个后端(example1.com)

  3. 然后将该图像路径(example1.com/images/img1.jpg)传递给发布请求。例如

    postdata = { imageurl:'example1.com/images/img1.jpg', 图片尺寸:“ 1200”, }

  4. 只有example1.com能够访问您的本地图像路径(例如:c://temp/img1.jpg)。因此,您应该将该图像上传到服务器1(example1.com)

  5. 或者将图像转换为Blob类型并发送。这样,您无需将图像保存在server1(example1.com)中。