1
votes

I'm trying to upload a Product image using the PrestaShop Web-service.

It is always returning the same error:

  <?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>

My current code looks like this:

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'
    }
});

I've tried with different encoding options, sending as 'Content-Type': 'image/jpeg', etc.

Thank you all for your time.

2

2 Answers

1
votes

This error is triggered by the writePostedImageOnDisk() method in /classes/webservice/WebserviceSpecificManagementImages.php.

Usually it can be due to:

  • Lack of permissions on your PHP /tmp folder and/or _PS_TMP_IMG_DIR_
  • $_FILES['image']['tmp_name'] being empty
  • Some issues with the customized_data table (in case this image is attached to a customized product)

This works for me (using PHP, not Node.js though):

<?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);

I hope this helps!

1
votes

I've been able to fix by sending the image as if it were uploaded by a form:

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);

}