🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

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!
Sep 4, 2020, 12:07 PM
try setting this in your _rawContent (block content) query
_rawContent(resolveReferences: { maxDepth: 20 })
Sep 4, 2020, 12:33 PM
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?
Sep 4, 2020, 12:40 PM
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
Sep 4, 2020, 12:40 PM
so that would be “bodyRaw” I belive
Sep 4, 2020, 12:41 PM
thanks but i found another solution using
@sanity/asset-utils
Sep 4, 2020, 12:48 PM
_const_ parts = parseAssetId(_file_.asset._ref);

_const_ url = buildFileUrl(parts, options);
Sep 4, 2020, 12:48 PM
this kindof works now, but no idea, if this is the official way to do things… Seems like documentation lacks this
Sep 4, 2020, 12:48 PM
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
🙂
Sep 4, 2020, 12:49 PM
Thanks for your help 🙂
Sep 4, 2020, 12:50 PM
I’d check out image-url instead :) https://www.sanity.io/docs/presenting-images
Sep 5, 2020, 7:59 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?