Trouble downloading blobs in tauri app on mac

137 Views Asked by At

I have an endpoint that I call that returns a download link to a blob. I then create an anchor tag and click it to download the content of that anchor tag. In my browser and on windows it works well. The anchor tag is clicked, and the content is downloaded. In my Mac desktop tauri application, it does not work. I get this error: "The download attribute on anchor was ignored because its href URL has a different security origin".

How can I make it work on Mac as well?

This is the code:

fetch(downloadlinkResponse.some.downloadLink)
            .then(response => response.blob())
            .then(blob => {
                var link = document.createElement("a");
                //link.href = URL.createObjectURL(blob);
                link.href = downloadlinkResponse.some.downloadLink;
                link.download = downloadlinkResponse.some.name;
                document.body.appendChild(link);
                link.click();
                link.remove();
            })
            .catch(error => console.error(error));

When I console.log the link.href, copy it and paste it in my browser, the content is downloaded, both in Safari and Chrome.

I have also tried creating an ObjectURL, as you can see, but that results in different Webkit errors.

Every possible "solution" I come across suggests it's CORS related, and I have tried a ton of CORS related stuff on my server, but nothing seems to work.

The anchor-tag error makes me think it is a frontend issue, but I have no idea how to get around it.

1

There are 1 best solutions below

0
On

Stood in front of the same problem, I solved it now for Tauri beta. This may help you:

my new imports:

import { save } from '@tauri-apps/plugin-dialog';
import { writeTextFile, writeFile } from '@tauri-apps/plugin-fs';

my func uses a blob from my api:

generateMonthlyConcerts(): void {
    this.eventService.getMonthlyConcertPdf(this.starttimeOfMonth, this.endtimeOfMonth).pipe(take(1))
      .subscribe(async blob => {
        const readableStream = blob.stream();
        const stream = readableStream.getReader();
        let data: Uint8Array;
        while (true) {
          let { done, value } = await stream.read();
          if (done) {
            console.log('all blob processed.');
            break;
          }

          data = value;
        }
        const path = await save({
          defaultPath: 'Konzertprogramm_' + formatDate(this.starttimeOfMonth, 'MMMM_y', 'de-DE') + '.pdf',
          filters: [
            {
              name: 'PDF Filter',
              extensions: ['pdf']
            },
          ],
        });
        await writeFile(path, data);
      });
  }