NodeJs API为产品图像提供目标路径

时间:2018-06-02 19:15:32

标签: node.js bigcommerce

我使用big commerce API上传带有图片的产品。该产品由API成功创建,但图像不成功。我如何提供目的地路径?

我已经给出了如下目的地路径

https://store-9gk124wgzn.mybigcommerce.com/dev/product_images

但这不起作用。

const storage = multer.diskStorage({
   destination: 'https://store-9gk124wgzn.mybigcommerce.com/dev/product_images',
   filename: function(req, file, cb) {
       cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
   }
});

enter image description here

以下是我试图给出路径图像的完整代码,它将图像文件夹名称设置为buddha.jpg,但它不会传递图像。     const productCreated = function(createnewproduct){       的console.log(createnewproduct);       const deferred = q.defer();       const postDataOptions = {         网址:${BC_STORE_URL}/api/v2/products,         方法:' POST',         标题:{           '接受':' application / json',           '内容类型':' application / json',           '授权':'基本' +新缓冲区(BC_USER +':' + BC_TOKEN).toString(' base64')         },         杰森:是的,         body:createnewproduct       };       request(postDataOptions,(error,res,body)=> {         的console.log(主体);         if(!error&& res.statusCode == 201){           的console.log(createnewproduct);           deferred.resolve(createnewproduct);         }       });       return deferred.promise;     }

app.post('/product-created', (req, res) => {

  const createnewproduct = {
    "name": req.body.name,
    "price": req.body.price,
    "categories": [req.body.categories],
    "type": req.body.type,
    "availability": req.body.availability,
    "description": "This timeless fashion staple will never go out of style!",
    "weight": req.body.weight,
    "is_visible": true,
    "id": 549

  };


  productCreated(createnewproduct).then(result => {
    const postImgDataOptions = {
      url: `${BC_STORE_URL}/api/v2/products/${result.id}/images`,
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + new Buffer(BC_USER + ':' + BC_TOKEN).toString('base64')
      },
      json: true,
      body: {
        //http://psdsandbox.com/022/pillow.jpg
        "image_file": "images/buddha.jpg", // this image is put in public folder
        "is_thumbnail": true,
        "sort_order": 0,
        "description": "Hi this is shutter img"
      }
    };
    request(postImgDataOptions, (error, response, body) => {
      console.log(response.statusCode);
      if (!error && response.statusCode == 201) {
        res.send('Done');
      } else {
        res.send('Bad Request');
      }
    });



  });

});

1 个答案:

答案 0 :(得分:0)

您是否尝试过只使用product_images/

而不是使用destination: https://...使用类似这样的回调函数

destination: function (req, file, cb) {
    cb(null, 'product_images/')
}

更新:所以要上传图片,您必须使用formdata

var uploadData = {
    image_file: {
      value: fs.createReadStream("images/buddha.jpg),
      options: {
        filename: "buddha.jpg",
        contentType: "image/jpg"
      }
    }
  };
  var uploadOptions = {
    method: "POST",
    uri: `${BC_STORE_URL}/api/v2/products/${result.id}/images`,
    formData: uploadData
  };
  return request(uploadOptions).then(res => {
    res.send('Done');
 }).catch(function(err){
    res.send('Bad Request');
 })