How to allow both upload and reference existing documents in Sanity file field?

11 replies
Last updated: Aug 26, 2021
I feel like this is an easy solution but I just can’t find it. Just a simple file type but it currently only allows to upload a document and not use an existing one. In the block type I needed two separate annotation items to do this but in a simple file type can it set to allow uploading a new document or use an existing one?
AI Update

The file type in Sanity Studio v3 doesn't automatically provide both upload and browse/select functionality by default in the way you might expect. Let me clarify what's actually happening:

What the file field provides by default

When you define a basic file field, the Studio will show an upload interface that allows you to:

  • Upload new files (drag-and-drop or click to browse your local files)
  • Access files through configured asset sources

About asset sources

The key to accessing existing files is the options.sources property. This controls which asset sources are available to the file field. Asset sources can include:

  • Your Sanity asset library (the default source)
  • External services like Cloudinary, Unsplash, etc.
  • Custom asset sources you've built
{
  name: 'myFile',
  type: 'file',
  title: 'My File',
  options: {
    sources: [] // Configure which sources are available
  }
}

Why annotations need two separate items

In Portable Text annotations, you're working within inline text marks, which have different constraints than standalone fields. That's why you needed separate annotation definitions - one for uploading/linking new files and one for referencing existing file documents.

Accessing existing files

To browse and select from previously uploaded files in your Sanity asset library, you'll want to ensure your file field has access to the default Sanity asset source. The documentation mentions custom asset sources as a way to control and extend this functionality.

If you're not seeing the ability to browse existing files, check:

  1. That you have files already uploaded to your project
  2. Your options.sources configuration (if you've set it)
  3. That you're using a recent version of Sanity Studio v3

The file type works similarly to the image type in terms of asset management, but the documentation doesn't explicitly state that a "browse existing" button appears by default - it focuses more on the upload functionality and the ability to configure asset sources.

Show original thread
11 replies
Hi Peter. You didn’t miss anything! Asset source support for the File field is not currently available. However, the good news is that it was just merged into next , which means it should be part of the next release. In the meantime, files are available using the media plugin.
So glad it’s coming to a future release! Any rough timeline?
I actually don’t know, but there is this release forthcoming.
user U
Check out v2.16.0. 👍
user U
Check out v2.16.0. 👍
YES! 🏃‍♂️
It sure does work. The only thing is that a user has to select “Uploaded Images” even though it could be any other file type, in our case it’s mostly PDF’s so it’s odd for the user to select images in order to get to PDF’s. But this is a small thing in the grand scheme of things. This is awesome!
It sure does work. The only thing is that a user has to select “Uploaded Images” even though it could be any other file type, in our case it’s mostly PDF’s so it’s odd for the user to select images in order to get to PDF’s. But this is a small thing in the grand scheme of things. This is awesome!
I’m glad to hear that, Peter.
For the dropdown, could you give this a try in your schema file that’s using the
file
type?

import Default from 'part:@sanity/form-builder/input/file/asset-source-default'

export default {
  // ...
  {
    name: 'fieldName',
    title: 'Field Name',
    type: 'file',
    options: {
      sources: [Default],
    },
    // ...
  },
  // ...
}
Hi Peter. I’m told that the “Uploaded Images” regression for file types is resolved in 2.17.0, so you shouldn’t need to use my workaround if you upgrade to that version. 👍
What great timing! Today i was going to implement the workaround. Thank You. The Sanity team is a great team. 🙂

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?