How to upload and get URLs for multiple images in Sanity, and add captions to individual images.

10 replies
Last updated: Dec 18, 2020
Hello guys , i am new to sanity and i have a 2 little problems .. first one is i did this schema to upload multiple images but i can't figure out how to get their url.. this is the schema for multi images upload
{
      
name: 'ProjectImages',
      
title: 'Project Images',
      
type: 'array',
      
of: [{ type: 'image' }]
     
},
and this is how i tried to get the url in my react frontend

ProjectImages{
        
asset->{
       
_id,
       
url
      
}
     
}

my second problem is how to enable multi-select for image upload because i keep uploading image by image
please help me i will appreciate it so much
AI Update

Great questions! Let me help you with both issues.

Problem 1: Getting Image URLs from an Array

Your GROQ query is almost correct, but you need to account for the array structure. Since ProjectImages is an array, you need to query each item in the array:

ProjectImages[]{
  asset->{
    _id,
    url
  }
}

The key difference is adding [] after ProjectImages - this tells GROQ to iterate over the array and get the asset reference for each image.

This will return an array of objects like:

[
  {
    "asset": {
      "_id": "image-abc123...",
      "url": "https://cdn.sanity.io/images/..."
    }
  },
  {
    "asset": {
      "_id": "image-def456...",
      "url": "https://cdn.sanity.io/images/..."
    }
  }
]

If you want just the URLs in a flatter structure, you can use:

"projectImages": ProjectImages[].asset->url

This will give you a simple array of URL strings.

Problem 2: Multi-Select Image Upload

Unfortunately, Sanity Studio doesn't currently support native multi-select for image uploads in array fields. You have to upload images one at a time by default.

However, you can drag and drop multiple images at once! Instead of clicking the upload button repeatedly:

  1. Select multiple image files from your file explorer
  2. Drag them all together onto the image array field in Studio
  3. Sanity will process and add them all sequentially

This is much faster than clicking upload for each individual image. The drag-and-drop functionality works in modern versions of Sanity Studio (v3 and later) and is the recommended approach for bulk image uploads.

For more advanced multi-select functionality, you'd need to create a custom input component using Sanity's custom input widgets, but the drag-and-drop approach should solve your workflow issue without any custom code.

Hope this helps! Let me know if you run into any issues with these solutions.

Add
[]
 to traverse the array:
ProjectImages[]{
        asset->{
       _id,
       url
      }
     }
You can also use the
@sanity/image-url
 package to generate URLs from the
_id
 /
_ref
, that is without having to join in the asset document. https://www.sanity.io/docs/image-url
If you have an array of images, you can drag and drop multiple files onto it. We should probably improve the UX so that you can also get a file selector that allows multiple files though.
Thank you so much !!
user Y
Is there a way to add a caption field to individual images in this sort of set up?If I add a
string
type object to my array containing the images nothing shows up in the studio.Thank you!
user H
Send the schema where you have added the string.
user C

{

name: 'imageButtons',

type: 'object',

title: 'Image Buttons',

fields: [

{

name: 'title',

type: 'string',

title: 'Title'

},

{

type: 'array',

name: 'image',

title: 'Gallery Image',

of: [

{ type: 'image', options: { hotspot: true } },

{

name: 'caption',

type: 'string',

title: 'Caption',

options: {

isHighlighted: true _// <-- make this field easily accessible_

}

}

],

options: {

layout: 'grid'

}

}

]

}
You have to add the caption for the image in the fields array.
You can find an example here
https://www.sanity.io/docs/image-type
user C
Thank you Harshal. I initially tried that but then I only get a single caption field for the entire array of multiple images.What I'm trying to do is attach a unique caption to each image within my array - do you know if this is doable?
Thanks again!
{
  
'name': 'imageButtons',
  
'type': 'object',
  
'title': 'Image Buttons',
  
'fields': [
    
{
      
name: 'title',
      
type: 'string',
      
title: 'Title'
    
},
    
{
      
type: 'array',
      
name: 'image',
      
title: 'Gallery Image',
      
of: [
        
{ type: 'image', options: { hotspot: true },
        
fields: [{
          
name: 'caption',
          
type: 'string',
          
title: 'Caption',
          
options: {
            
isHighlighted: true // <-- make this field easily accessible
          
}
        
}]},
              
],
      
options: {
        
layout: 'grid'
      
}
    
}
  
]

}
works perfect, thank you!!

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?