'uxp' is not defined

I was trying to write a temporary file on disk, but in the downloadImage function ,I got that error.
“uxp is not defined”.

function xhrBinary(url) {                                       // [1]
    return new Promise((resolve, reject) => {                   // [2]
        const req = new XMLHttpRequest();                       // [3]
        req.onload = () => {
            if (req.status === 200) {
                try {
                    const arr = new Uint8Array(req.response);   // [4]
                    resolve(arr);                               // [5]
                } catch (err) {
                    reject(`Couldnt parse response. ${err.message}, ${req.response}`);
                }
            } else {
                reject(`Request had an error: ${req.status}`);
            }
        }
        req.onerror = reject;
        req.onabort = reject;
        req.open('GET', url, true);
        req.responseType = "arraybuffer";                       // [6]
        req.send();
    });
}


function applyImagefill(selection, file) {                             // [1]
    const imageFill = new ImageFill(file);                             // [2]
    selection.items[0].fill = imageFill;                               // [3]
}

async function downloadImage(selection, jsonResponse) {


	try {
        const photoUrl = jsonResponse.message;                                     // [2]
        const photoObj = await xhrBinary(photoUrl);                                // [3]
        const tempFolder = await fs.getTemporaryFolder();                          // [4]
        const tempFile = await tempFolder.createFile("tmp", { overwrite: true });  // [5]
        await tempFile.write(photoObj, { format: uxp.formats.binary });            // [6]
        applyImagefill(selection, tempFile);                                       // [7]
    } catch (err) {
        console.log("error")
        console.log(err.message);
    }

}

Is there anything I missed to add? Thank you for your support

Did you import uxp with something like const uxp = require('uxp');?

PS: Welcome to the forums :wave::slightly_smiling_face:

2 Likes

No, I didn’t know about that. It worked after importing that. Thank you so much.

2 Likes

I do, however, tend to believe (on second glance) that

format: uxp.formats.binary

should actually either be

format: uxp.storage.formats.binary

or

format: fs.formats.binary

(since you appear to already have imported a variable fs from uxp)

The fact that it already works is, shall we say, surprising or a rather happy coincidence :wink: