• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Download image file UXP Script

Explorer ,
May 02, 2023 May 02, 2023

Copy link to clipboard

Copied

Hi all,

 

I need to fetch an image file via network request and then insert that file into an inDesign document. I think I know how to do the insertion, but I'm having trouble establishing the file in the script. Due to the locked down nature of UXP file system access, I'm first opening (i.e. creating) a file and then attempting to write the image binary to it. It seems to complete without error, but the output file png is not opening an image and is considered an unsupported type by the operating system. Any tips would be appreciated!

 

async function fetchJson(url) {
return fetch(url)
.then((response) => {
return response;
});
}

var response = await fetchJson("http link to image src file");

const imageBlob = await response.blob()


const fs = require("uxp").storage.localFileSystem;
const fileToWrite = await fs.getFileForSaving("output.png");
fileToWrite.write(imageBlob.arrayBuffer());
TOPICS
Scripting , UXP Scripting

Views

571

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 03, 2023 May 03, 2023

Okay I solved it like this:

const fs = require("uxp").storage.localFileSystem;

async function fetchBuffer(url) {
return fetch(url)
.then((response) => {
return response.arrayBuffer();
});
}

var response = await fetchBuffer(<http link to image src file>);


const tempFolder = await fs.getTemporaryFolder();
const tempFile = await tempFolder.createFile("output.png", { overwrite: true });
await tempFile.write(response);

Votes

Translate

Translate
Explorer ,
May 03, 2023 May 03, 2023

Copy link to clipboard

Copied

LATEST

Okay I solved it like this:

const fs = require("uxp").storage.localFileSystem;

async function fetchBuffer(url) {
return fetch(url)
.then((response) => {
return response.arrayBuffer();
});
}

var response = await fetchBuffer(<http link to image src file>);


const tempFolder = await fs.getTemporaryFolder();
const tempFile = await tempFolder.createFile("output.png", { overwrite: true });
await tempFile.write(response);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines