Search code examples
javascriptnode.jsbase64salesforce

No preview for the image I uploaded from nodejs code to salesforce system


According to this blog, I'm trying to upload a file to salesforce.

This is part of ny code:

const base64Content = fs.readFileSync('/home/***/Desktop/version.png', { encoding: 'base64' });
const fileName='version.png';

request.post({
    url: `${res.instance_url}/services/data/v49.0/sobjects/ContentVersion`,
    auth: {
        bearer: res.access_token
    },
    formData: {
        entity_content: {
            value: JSON.stringify({PathOnClient: fileName}),
            options: {
                contentType: 'application/json'
            }
        },
        VersionData: {
            //value: base64Content,
            value: `data:image/png;base64,${base64Content }`,
            options: {
                filename: fileName,
                contentType: 'application/octet-stream'
            }
        }
    }
}, (err, response) => {
    if (err) callbackerr(err);
    callback(response)
})

The image is uploaded, but there is no preview in salesforce UI. I try to download the image -

For PNG image I got:

Fatal error reading PNG image file: Not a PNG file

For JPEG:

Error interpreting JPEG image file (Not a JPEG file: starts with 0x64 0x61)

What am I missing? I aslo tried replacing the 'application/octet-stream' with 'image/png'.

querying in the developerConsole like the following:

SELECT id, ContentDocumentId, FileExtension, FileType FROM ContentVersion

gives FileExtension png and FileType PNG.


Solution

  • I found the problem. The image should be buffer and not base64...

    const buffer = fs.readFileSync('/home/***/Desktop/version.png');