Create a download link

Hello,

I am trying to create a link so users can download a file from my project. I created the link like this:
Screen Shot 2021-12-23 at 7.53.11 AM

Which resulted in this:

Yet when the link is clicked, the file opens in the browser.

What am I doing wrong?

1 Like

Hello @john

Add the following parameters to the asset URL:

?dl=assetexample.pdf

That would make the url be

https://www.datocms-assets.com/58581/1640218505-assetexample.pdf?dl=assetexample.pdf

This will create a download link for that asset, not a “visualisation link”

The download attribute on the anchor tag is unnecessary, as it doesn’t support CORS download requests

Let me know if this helps!

Bingo! Thank you!

An extension to this question: can you direct me to documentation on how to create a link that is expires, or requires a user to be logged in / have an account / etc?

We don’t have a native solution for that, sorry, but it’s something you can do on your server-side with something like this: files-temp-urls - npm
Or if you prefer you can also implement your own solution

Thank you Marcelo!

1 Like

It would be great to be able to get download urls generated in grapql. So I don’t need to put these assumptions to my project.

I would like to propose a download parameter for the url field of FileField:

query {
    upload(filter: {filename: {matches: {pattern: "some-file.pdf"}}}) {
        url(download: "Some File Name.pdf")
    }
}

What do you think?

Hello @mvengelshoven

It does seem like a good suggestion to me, i’ll let the dev team know and see if we can do something about it :slight_smile:

1 Like

You can already do this:

{
  upload {
    url(imgixParams:{ dl: "Some File Name.pdf" })
  }
}

3 Likes

Never thought about looking into imgixParams for the download link. :sweat_smile: But, as I see just now, the download links are an imgix feature, now this makes sense to me.

Thank you!