Trouble with file assets in Sanity Studio and how to upload them

10 replies
Last updated: Apr 14, 2020
Hi, I'm trying to get a File schema to work and am having no luck. I followed the docs: Files
When you need to upload a non-image file (say a PDF or a zip file) use the 
/assets/files/<dataset>
 endpoint. The response is similar to the one you get from an image, but the type will be 
fileAsset
.
But this is all I get. I see it in the graphql query, just nowhere in studio or anywhere else...
Apr 14, 2020, 12:57 PM
Hi User, I’m sorry the file assets are giving you some trouble. If I understand you correctly, you’re able to upload them successfully via the endpoint and you can also query them? It’s just that you can’t see them in the Studio itself? If you have the Vision plugin installed, are you able to query them using
*[_type == 'file']
? What’s an example output of a file when queried with GraphQL?
Apr 14, 2020, 3:24 PM
I haven't tried uploading via the endpoint. I can however see the sanityFileAsset in GraphiQL
Apr 14, 2020, 3:28 PM
There’s no need to upload via the endpoint if you’re happy enough doing so within the Studio itself 🙂 The
/assets/files/<dataset>
endpoint is used to upload images via the HTTP API. In your case, it looks like you were trying to add the endpoint to your
rd-sanity.sanity.studio
URL, which is independent from the HTTP API and therefore the endpoint above.
The
rd-sanity.sanity.studio
URL is simply where you can access the Sanity Studio to manage your content. The HTTP API with its different endpoints would have a different URL, such as
<http://your-project-id.api.sanity.io/v1/assets/images/production|your-project-id.api.sanity.io/v1/assets/images/production>
. But as said above, you won’t necessarily need this to upload files. I hope the distinction is a bit clear 🙂

sanityFileAsset
and
allSanityFileAsset
are included by default in GraphiQL but do not necessarily mean that you have available files. They would also show if you have no files at all uploaded yet.
Let’s see if we can figure out why it’s not working. Could you share your file schema?
Apr 14, 2020, 3:42 PM
Thanks User, here's the file schema:
export default {
    
title: 'Rescource',
    
name: 'rescource',
    
type: 'file',
    
fields: [
        {
            
name: 'categories',
            
type: 'array',
            
title: 'Categories',
            
of: [
                {
                
type: 'reference',
                
to: {
                    
type: 'category'
                
}
            
}
        
]
        
},
      
{
        
name: 'description',
        
type: 'string',
        
title: 'Description'
      
},
      
{
        
name: 'author',
        
type: 'reference',
        
title: 'Author',
        
to: {type: 'author'}
      }
    ]
  }
Apr 14, 2020, 3:45 PM
Thanks User — have you added
rescource
to your content schema? And have you added it as a field to any of your document types?
Apr 14, 2020, 4:45 PM
Yes, I imported it to the main schema and I'm beginning to see where I went wrong. I had assumed that the File(Rescource) would be a standalone inpun in studio. I'm going to create a new download schema to include the file in and see if that works.
Apr 14, 2020, 5:07 PM
*input
Apr 14, 2020, 5:07 PM
That sounds like a good direction, as you’ll probably need to include it in a document type to start seeing it in your Studio 🙂
Apr 14, 2020, 5:09 PM
Thanks User. I appreciate your help.
Apr 14, 2020, 5:14 PM
Hey User, I got the fil into a document. Now, I'm trying to figure out how to get the "upload" button like in the image upload. My goal is to make Documents uploadable to a front end that's easy for anyone.Thanks again
Apr 14, 2020, 7:50 PM

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?