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());
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);
Copy link to clipboard
Copied
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);