我正在使用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));
}
});
这是我试图给出路径图像的完整代码,它将图像文件夹名称设置为buddha.jpg但它没有传递图像 . const productCreated = function(createnewproduct){console.log(createnewproduct); const deferred = q.defer(); const postDataOptions = {url: ${BC_STORE_URL}/api/v2/products
,方法:'POST', Headers :{_ 'Accept':'application/json',_ 'Content-Type':'application/json',_ 'Authorization':'Basic '新缓冲区(BC_USER ':' BC_TOKEN).toString('base64')},json:true,body:createnewproduct}; request(postDataOptions,(error,res,body)=> {console.log(body); 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 回答
你试过只用
product_images/
吗?而不是使用
destination: https://...
使用类似这样的回调函数UPDATE: 所以要上传图片,你必须使用formdata