0
const monitorAnimation = (characterId) => {
    const monitorUrl = `somesite/api/v1/characters/${characterId}/monitor`;
    const monitorInit = {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${bearer}`,
            'X-Api-Key': 'somesite1'
        }
    };
    return fetch(monitorUrl, monitorInit)
        .then((res) => {
            switch (res.status) {
                case 404: {
                    const errorMsg = ('ERROR: Monitor got 404 error: ' + res.error + ' message=' + res.message);
                    console.error(errorMsg);
                    throw new Error(errorMsg);
                } break
                case 202:
                case 200: {
                    return res.json()
                } break
                default:
                    throw new Error('Response not handled', res);
            }
        }).then((msg) => {
            switch (msg.status) {
                case 'completed':
                    console.log('Downloading: ', msg.job_result);
                    downloadingTab.location.href = `${msg.job_result}&download_folder=D:/anim/animations`;
                    return msg.job_result;
                    break;
                case 'processing':
                    console.log('Animation is processing... looping');
                    return monitorAnimation(characterId);
                    break;// loop
                case 'failed':
                default:
                    const errorMsg = ('ERROR: Monitor status:' + msg.status + ' message:' + msg.message + 'result:' + JSON.stringify(msg.job_result));
                    console.error(errorMsg);
                    throw new Error(errorMsg);
            }
        }).catch((e) => Promise.reject("Unable to monitor job for character " + characterId + e))
}

This line

downloadingTab.location.href = `${msg.job_result}&download_folder=D:/anim/animations`;

was in the original

downloadingTab.location.href = msg.job_result;

but once i changed it to my folder d:/anim/animations i'm getting errors and its not downloading it.

how to change the right way the download folder?

I tried the above to change the download folder.

Shelly Ron
  • 27
  • 3
  • I dont think this is possible. [This post](https://stackoverflow.com/questions/34870711/download-a-file-at-different-location-using-html5) will give some insights. – kiranvj Jul 09 '23 at 13:41
  • Does this answer your question? [Download A File At Different Location Using HTML5](https://stackoverflow.com/questions/34870711/download-a-file-at-different-location-using-html5) – Jon P Jul 09 '23 at 23:11

1 Answers1

2

There is no way to change the download folder location in JavaScript directly because it's controlled by the browser settings and the user preferences. It would be a security risk if a web page could access or modify the user's file system without their consent.

You can use the download attribute on the anchor element to specify a default file name for the downloaded file, but not the folder location.

For example:

<a href="file.pdf" download="report.pdf">Download report</a>

I'm not sure what you are trying to achieve, but copying a file to another folder after it is downloaded is also not possible in JavaScript directly. You would need to use some external program or library that can access the file system, such as Node.js or Electron. However, this would require the user to install and run your program on their machine, which may not be feasible or desirable for your use case.

Alternatively, you can try to use the File System Access API, which is a new web standard that allows web pages to read and write files and directories on the user's local device with their permission. However, this API is still experimental and not widely supported by all browsers. You can check the compatibility table.

Here is an example of how you could use the File System Access API to copy a file to another folder:

const fileHandle = await window.showOpenFilePicker();
const dirHandle = await window.showDirectoryPicker();
await dirHandle.requestPermission({ mode: 'readwrite' });
const newFileHandle = await dirHandle.getFileHandle(fileHandle.name, { create: true });
const writable = await newFileHandle.createWritable();
await writable.write(await fileHandle.getFile());
await writable.close();

This code requires the user to select the file and the destination directory manually using a file picker dialog. There is no way to automate this process or bypass the user's consent.

Pluto
  • 4,177
  • 1
  • 3
  • 25
  • then my best shot will be to wait for a file to download then copy it to the folder i want. any ideas how to do it in the script code? one a file download finished copy it to another folder. – Shelly Ron Jul 09 '23 at 17:01
  • I updated my answer. But I'm not sure what you want. – Pluto Jul 09 '23 at 22:54