最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Download S3 file from pre-signed URL without popup blocker - Stack Overflow

matteradmin7PV0评论

I have a Meteor application where I'm downloading files from S3 using pre-signed URLs (need to be generated with an API call).

I was having an issue with popup blockers preventing a new tab from opening with the url generated by the AWS-SDK so I changed my code to the following:

downloadDocument(document, event) {
    // open tab immediately to prevent popup blocker
    const myNewTab = window.open();

    // call method to generate url
    Meteor.call('Events.Methods.Document.Download', { key: document.key, eventId: event._id }, (error, res) => {
      if (error) { ... } // removed handle error code

      // if url generated, set tab location to url
      if (res) myNewTab.location.href = res;

      // auto close the tab after 1 second
      myNewTab.setTimeout(() => { myNewTab.close(); }, 1000);
    });
}

This code is working for the most part but it doesn't feel very clean. Also if the API call ever takes more than 1 second (slow internet) then the tab will close before the download begins

How can I change this so that I can wait for the download to happen, before closing the tab? Or a similar solution that would result in me ensuring the downloads always go through without popup blockers being an issue?

Thanks

I have a Meteor application where I'm downloading files from S3 using pre-signed URLs (need to be generated with an API call).

I was having an issue with popup blockers preventing a new tab from opening with the url generated by the AWS-SDK so I changed my code to the following:

downloadDocument(document, event) {
    // open tab immediately to prevent popup blocker
    const myNewTab = window.open();

    // call method to generate url
    Meteor.call('Events.Methods.Document.Download', { key: document.key, eventId: event._id }, (error, res) => {
      if (error) { ... } // removed handle error code

      // if url generated, set tab location to url
      if (res) myNewTab.location.href = res;

      // auto close the tab after 1 second
      myNewTab.setTimeout(() => { myNewTab.close(); }, 1000);
    });
}

This code is working for the most part but it doesn't feel very clean. Also if the API call ever takes more than 1 second (slow internet) then the tab will close before the download begins

How can I change this so that I can wait for the download to happen, before closing the tab? Or a similar solution that would result in me ensuring the downloads always go through without popup blockers being an issue?

Thanks

Share Improve this question asked Oct 28, 2017 at 17:58 SeanSean 2,7292 gold badges20 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You are always going to run afoul of pop-up blockers if you open a new window.

What you should do is generate an <a href="my-custom-server-generated-url" download> link with the download property, which will force a download without needing a new window.

Then you also don't need to close the window on a timer (which wasn't a good approach in the first place)

This was happening only in Safari, so we switched to always downloading the file instead of opening in a new window in Safari/mobile.

Post a comment

comment list (0)

  1. No comments so far