-1

I would execute following two tasks one after the other:

  1. download image save to disk
  2. call a backend method and then navigate to Instagram
<a
  data-lightbox-anima="fade-top"
  className={`btn`}
  download={`${organizationShortId}-${programId}`}
  href={`/t44-post-cover/${organizationShortId}-${programId}`}
  onClick={(e) => {
    dispatch(
      createShare({
        props: {
          tempUserShortId: getTempUserShortId(),
          postId,
        },
        andThen: (_) => {
          if (
            confirm(
              "Megnyitom az Instgramot, s megosztom az utoljára mentett animációt!"
            )
          ) {
            router.push(`https://www.instagram.com`);
          }
        },
      })
    );
  }}
>
  Insta
</a>

Unfortunately in mobil only download get started and finished, but confirm dialog will not appear anymore. What can I do to do both task?

enter image description here

enter image description here

János
  • 32,867
  • 38
  • 193
  • 353
  • that's Safari right? I think it's Apple related: https://discussions.apple.com/thread/251522753 – GrafiCode Sep 02 '22 at 11:28
  • yes, Safari, but it is not a problem for me if file goes to "Download" folder, I know it exist in Desktop, I never heared it exist also in mobil, but the challange, how to continue with task 2, call backend and navigate – János Sep 02 '22 at 11:29
  • I have same challenge in Chrome – János Sep 02 '22 at 11:34

1 Answers1

1

To my best knowledge, there is no browser event that could tell you when a download initiated by your page has finished.

You'll need to initiate a download, wait for a bit (to hope it has started and/or finished), and then continue doing what you want to do:

export default function initiateDownload(url, download, delay) {
  return new Promise((resolve) => {
    const aElement = document.createElement('a');
    aElement.href = url;
    aElement.download = download;
    document.body.appendChild(aElement);
    aElement.click();
    setTimeout(() => {
      const parentNode = aElement.parentNode;
      if (parentNode) {
        parentNode.removeChild(aElement);
      }
      resolve();
    }, delay);
  });
}

...

await initiateDownload(
    `/t44-post-cover/${organizationShortId}-${programId}`,
    `${organizationShortId}-${programId}`,
    1000,  // wait for a second
);

... do other things...
AKX
  • 152,115
  • 15
  • 115
  • 172
  • seems if this is set: `download={`${organizationShortId}-${programId}`}` than `onClick` will not be called. Do you have any idea, how I could start both? – János Sep 02 '22 at 11:38
  • 1
    Sure. See my edit. – AKX Sep 02 '22 at 11:44