How can I download the contents from a URL through my Tauri app on Mac?

353 views Asked by At

I have built a Tauri app that works like a document center. I upload files to blob containers, and I can see them in my documents. From here, I want to be able to download them again, which is working very well in browsers, as well as the Tauri desktop app on my Windows machine. The problem however, seems to be in my Mac client.

This is the code I had originally in my TypeScript:

export async function downloadFile(fileId, parentId) {
    var downloadlinkResponse = await getDownloadLink(fileId, parentId);
    if (downloadlinkResponse.none) {
        alert(downloadlinkResponse.none.public_message ?? "File could not be downloaded");
        return;
    }

    var link = document.createElement("a");
    link.setAttribute("download", "");
    link.href = downloadlinkResponse.some.downloadLink;
    document.body.appendChild(link);
    link.click();
    link.remove();
}

This worked well, however, on my Mac, I got this error: "The download attribute on anchor was ignored because its href URL has a different security origin"

The getDownloadLink returns a SasURI correctly.

To try and fix this, I implemented a slightly different function:

function downloadBlob(url, filename) {
    fetch(url, {
      headers: new Headers({
        'Origin': location.origin
      }),
      mode: 'cors'
    })
    .then(response => response.blob())
    .then(blob => {
      let blobUrl = window.URL.createObjectURL(blob);
      let link = document.createElement('a');
      link.setAttribute('download', filename);
      link.href = blobUrl;
      document.body.appendChild(link);
      link.click();
      link.remove();
      window.URL.revokeObjectURL(blobUrl);
    })
    .catch(error => {
      console.error(error);
    });
  }

And replaced the final 6 lines of the first bit of code with:

downloadBlob(downloadlinkResponse.some.downloadLink, downloadlinkResponse.some.name);

This still works very well in browsers and on my Windows desktop app, however, my Mac client does not download the file - or at least I can't find the file anywhere. I don't get any errors in console, so it's difficult to say if anything goes wrong without my knowledge.

1

There are 1 answers

0
pashokitsme On

I had the same problem on v2, so just fixed it myself
Added on_download_started and on_download_completed setters to window builder. You need to set at least one, but i think you also need to modify destination path like {downloads_dir}/{filename} to download file into downloads directory
Here: https://github.com/tauri-apps/tauri/pull/8159