I have files on google cloud storage and i want to add a button so that users can download those files to their pc. I tried the usual:
<a href="imageUrlHere" download="testImage">Download</a>
But it only opens the image file in the same tab.
Everything running on the front end of the app by the way.
2
Answers
Since you already have the download URL of the File stored in Cloud Storage, the following function will do the trick:
And you can call it with
Or, better, by adding a listener:
HTML
JavaScript
An alternative to using HTML attributes would be to set the
Content-Disposition
header toattachment; filename="file.ext"
so you can use thedownloadURL
so whenever the URL is opened in new tab, it’ll be downloaded. This can be set in the metadata when uploading the file or you can useupdateMetadata()
function to add it for existing files.You just need to redirect the user to a new tab. If you don’t specify the
filename
, the browser will prompt user to enter one.