How to use the result of getFileUrl

I have added an onClick event handler for an element in my page. In the event handler, I run getFileUrl to get a download url.

However, my question is that I am not sure how to use the returned download url. I know if you copy&paste the download url in browser, the file will automatically be downloaded for you. But I want to automate the “copy&paste download url in browser” step in code. How can I do it?

Currently, I have 2 ideas.

First, run wixLocation.to(fileUrl)
Second, run $w(#elementId).link = fileUrl

which one of them works properly? what’s the differences between them? are there other solutions better than those 2?

It depends on the flow which is not so clear from your description.
If you wish to get the url, and after that to let the user click a button to download - use the .link= option.
But if you wish to get the url and automatically redirect to this link without a manual click - use wixLocation.