我正在尝试使用PrestaShop网络服务上传产品图像。
它总是返回相同的错误:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[66]]></code>
<message><![CDATA[Unable to save this image]]></message>
</error>
</errors>
</prestashop>
我当前的代码如下所示:
const url = this.options.url
+ '/api/images/products/'
+ piezaSchema.querySelector('product>id').childNodes[0].nodeValue
+ '?ws_key='
+ this.options.api;
const file = require('fs').readFileSync(require('path')
.resolve(this.options.ruta
+ '/images/'
+ image.getAttribute('fichero')));
const resp = await fetch(url, {
method: 'POST',
body: 'image=' + file,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
我尝试过不同的编码选项,按'Content-Type': 'image/jpeg'
发送等。
谢谢大家抽出宝贵时间。
此错误
由 /classes/webservice/WebserviceSpecificManagementImages.php
中的 writePostedImageOnDisk()
方法触发。
通常可能是由于:
- 缺少对 PHP
/tmp
文件夹和/或_PS_TMP_IMG_DIR_
的权限 -
$_FILES['image']['tmp_name'] being empty
customized_data
表的一些问题(如果此图像附加到自定义产品(
这对我有用(使用PHP,而不是Node.js(:
<?php
include(__DIR__.'/config/config.inc.php');
/* Connect to the PrestaShop Web-service */
define('PS_SHOP_URL', 'http://localhost/prestashop');
define('PS_WS_AUTH_KEY', 'YOURWSKEY');
/* Local path to the image to upload */
$image_path = './test.png'; // Can either be JPEG, PNG, etc.
/* Image upload */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, PS_SHOP_URL.'/api/images/products/1');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => curl_file_create($image_path)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
echo '<pre>'.print_r($response).'</pre>'; // Should echo '1'
curl_close($ch);
我希望这有帮助!
我已经能够通过发送图像来修复,就好像它是通过表单上传的一样:
async uploadImage(fichero, url) {
const form = new FormData();
const filePath = require('path').resolve(this.options.ruta + '/images/' + fichero);
const file = new File([await fetch(filePath).then(r => r.blob())], fichero, {type: 'image/jpeg'});
form.append('image', file);
const options = {
method: 'POST',
body: form
};
fetch(url, options);
}