我正在上传一个产品的图像使用大商业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
,method:“post”,headers:{“accept”:“application/json”,“content-type”:“application/json”,“authorization”:“basic”+new Buffer(BC_USER+“:”
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');
}
});
});
});
您是否尝试只使用product_images/
?
而不是使用destination:https://...
使用回调函数,如下所示
destination: function (req, file, cb) {
cb(null, 'product_images/')
}
Update:所以要上传图像,您必须使用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');
})