I need to download some image files by https:
const uxpStorage = require('uxp').storage,
fs = uxpStorage.localFileSystem;
const tmpFolder = await fs.getTemporaryFolder();
// download from sourceUrl
const resp = await fetch(sourceUrl);
const buffer = await resp.arrayBuffer();
// write to local file
const tmpFile = await tmpFolder.createEntry(fileName, {overwrite: true})
await tmpFile.write(buffer, {format: uxpStorage.formats.binary})
This results often in corrupted image files. If I insert a pause after getting the arraybuffer, it mostly works, but that can’t be a proper solution! Like this:
…
const buffer = await resp.arrayBuffer();
await timeout(1500);
…
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
I tried to replace the fetch by XMLHttpRequest, using the function from an XD UXP tutorial, but that doesn’t help.
//const resp = await fetch(sourceUrl);
//const buffer = await resp.arrayBuffer();
const buffer = await xhrBinary(sourceUrl);
Any ideas or suggestions about this?