How to get image URLs from array field and enable multi-image upload in Sanity?

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

Welcome to Sanity! Great questions - these are common when starting out with arrays of images. Let me help you with both issues.

Getting URLs from an Array of Images

The issue with your GROQ query is that you need to add [] to traverse the array. Here's the corrected query:

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

The [] tells GROQ to iterate through each item in the array and fetch the asset information for each image.

Even better approach: You can use the @sanity/image-url package to generate URLs directly from the asset reference without having to expand the asset document in your query. This is the recommended approach as it gives you more control over image transformations (size, format, quality, etc.).

Here's how to set it up:

import imageUrlBuilder from '@sanity/image-url'
import { client } from './client'

const builder = imageUrlBuilder(client)

function urlFor(source) {
  return builder.image(source).url()
}

// Then use it with your images:
ProjectImages.forEach(image => {
  const url = urlFor(image)
  console.log(url)
})

Enabling Multi-Select for Image Upload

Good news! Sanity already supports this - you just need to drag and drop multiple files onto the array field. Select multiple image files from your file explorer and drag them all at once onto the array input in the Studio, and they'll all be uploaded together.

The UX could be clearer (and the Sanity team is aware of this), but the functionality is there. Unfortunately, there isn't currently a way to use the file picker dialog to select multiple files at once - drag-and-drop is the way to go for now.

Bonus: Adding Captions to Individual Images

If you want to add captions or other metadata to each image in your array, you can add custom fields like this:

{
  name: 'ProjectImages',
  title: 'Project Images',
  type: 'array',
  of: [{
    type: 'image',
    fields: [
      {
        name: 'caption',
        type: 'string',
        title: 'Caption',
        options: {
          isHighlighted: true // Makes it easily accessible
        }
      }
    ]
  }]
}

Then query it like:

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

Hope this helps! Let me know if you have any other questions.

Show original thread
10 replies
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?