Open Window using flow and JS

I’m using DO spaces and the S3 server connect and trying to create a download button in a table.

I’d like for the download to open in a new tab in the browser each time. The button runs the server connect to get the DO url. On success it runs a JS function to open the url in a new tab. This works perfectly the first time it runs. However if I try to view a second document I get:

The on success code is:
dmx-on:success="run({runJS:{function:'openTab',args:[download.data.signDownloadUrl1,download.data.id],name:'openTab'}})"

JS function is:

function openTab(url, id) {
    console.log(url);
    console.log(id);
    openTab = window.open(url, "_blank");
}

This looks to be a security measure, but it seems odd it should run once. Any thoughts on how to work around it?

Personally, I prefer to set up a Server Action for the file download (in which I use an insert step to add a line to my DB with the access date/time/ipaddress/userid). You can also perform permission checks for logged in users too.

Then on the page you don’t need a JS function, just a anchor button with a href (in your case, probably dynamic attribute href) that points to the Server Action (add a SC route to make it shorter and more presentable)

The Server Action has a $_GET variable for a unique file id or code. I then use a single query to look up the details of the file, insert an access request into the DB, check permissions, before downloading the file.

Thanks Ben, I’m trying to avoid the second button click if possible.

I can get it to work on success using the browser goto, but this opens in the same tab. Or using the anchor button which opens in a new tab but needs 2 clicks to get the document. One to get the url the other to open it.

The JS function, in theory, should mean I can get a new window from a single click. It almost works, just can’t find a way past the last bit!

Edit- just re-read your post. Using the server action as the href would bypass the second click…I’ll give that a go! Thanks!

1 Like

It works really well for me - you could also, if useful, share file urls like

somedomain.com/doc/ABC123D

Where the ABC… is the GET parameter of the route and equals the file’s unique code/id