How to Create a Download URL For Files

10 replies
Last updated: Sep 5, 2020
Is there a good way to create the download url for files, that are part of a block-content? I use graphql. the only info i have is the _ref, but how to get the url? With images its simple using urlBuilder… but not working for file assests. Any help is highly appreciated!
try setting this in your _rawContent (block content) query
_rawContent(resolveReferences: { maxDepth: 20 })
in my graphql query i don’t have a _rawContent field… it looks like :
query allPage {
allPage{
_id
slug{current}
title
navigationtitle
bodyRaw
}
}

Where should i put maxDepth?
I just meant whatever you use to get your block content data 🙂, I usually use raw fields for that, but I guess you could use normal fields
I have only used graphql from Sanity with gatsby, and the gatsby sanity plugins give new names to some fields, so I may have differently named fields
so that would be “bodyRaw” I belive
thanks but i found another solution using
@sanity/asset-utils
_const_ parts = parseAssetId(_file_.asset._ref);

_const_ url = buildFileUrl(parts, options);
this kindof works now, but no idea, if this is the official way to do things… Seems like documentation lacks this
Great! resolveReferences should also work, instead of giving you the reference it should give you the data that reference contains.
But it seems like that is exactly what you do with asset utils as well
🙂
Thanks for your help 🙂
I’d check out image-url instead :) https://www.sanity.io/docs/presenting-images

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?