Show downloadable file URL after PDF upload in Sanity
Yes, absolutely! You can show the downloadable file URL after a PDF is uploaded. There are a couple of approaches you can take:
1. Display the URL in your frontend
When you query the file field with GROQ, you can get the URL like this:
*[_type == "yourDocumentType"] {
"fileUrl": yourFileField.asset->url
}Then in your frontend, you can display it as a download link. Important tip: Sanity files are hosted on a different origin, so the standard HTML download attribute won't work. Instead, use the ?dl query parameter:
<a href={fileUrl + '?dl'}>Download PDF</a>The ?dl parameter tells Sanity's CDN to serve the file as a download instead of trying to display it in the browser.
2. Show the URL directly in Studio (for editors)
If you want content editors to see the download URL right in the Studio after uploading, you can create a custom input component. This would wrap the default file input and display the URL below it once a file is uploaded.
Here's a basic example:
import { FileInput } from 'sanity'
export default function FileWithUrl(props) {
const fileUrl = props.value?.asset?._ref
? `https://cdn.sanity.io/files/${projectId}/${dataset}/${props.value.asset._ref.replace('file-', '').replace('-pdf', '.pdf')}`
: null
return (
<div>
<FileInput {...props} />
{fileUrl && (
<div style={{marginTop: '1em'}}>
<a href={fileUrl + '?dl'} target="_blank" rel="noopener noreferrer">
Download: {fileUrl}
</a>
</div>
)}
</div>
)
}You can find more details in the Sanity documentation on file types and this community answer about adding downloadable file URLs.
Show original thread4 replies
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.