首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用带节点的PrestaShop网络服务上传产品图像

使用带节点的PrestaShop网络服务上传产品图像
EN

Stack Overflow用户
提问于 2019-05-30 16:22:32
回答 2查看 1.2K关注 0票数 1

我正在尝试使用PrestaShop网络服务上传产品图像。

它总是返回相同的错误:

代码语言:javascript
复制
  <?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>

我当前的代码是这样的:

代码语言:javascript
复制
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',等等。

感谢大家抽出宝贵的时间。

EN

回答 2

Stack Overflow用户

发布于 2019-05-31 02:53:52

此错误由/classes/webservice/WebserviceSpecificManagementImages.php中的writePostedImageOnDisk()方法触发。

通常是由于:

  • 缺少对您的PHP /tmp文件夹的权限和/或customized_data表的_PS_TMP_IMG_DIR_
  • $_FILES['image']['tmp_name'] being empty
  • Some问题(如果此图像附加到自定义产品)

PHP这对我有效(使用,而不是Node.js ):

代码语言:javascript
复制
<?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);

我希望这能帮到你!

票数 1
EN

Stack Overflow用户

发布于 2019-05-31 15:30:56

我已经能够通过发送图片来修复它,就像它是通过表单上传的一样:

代码语言:javascript
复制
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);

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56374395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档