Programmable Media

Upload assets with Server Actions in a Next.js app (video tutorial)

Last updated: Dec-11-2024

Overview

Add asset upload capabilities to your Next.js application using Server Actions with the Cloudinary Node.js SDK.

Video tutorial


View the code
You can find the code from this tutorial in GitHub.

Tutorial contents

This tutorial presents the following topics. Click a timestamp to jump to that part of the video.

Introduction to Server Actions

Jump to this spot in the video  0:00 Server Actions allow you to add server-side capabilities to your Next.js or React app. In this tutorial you'll learn how to upload files to Cloudinary inside a Server Action.

Using Server Components

Jump to this spot in the video  0:12 Server Actions are built on top of React Actions and give you server-side capabilities inside of a component. Use Server Components to make your app more performant and leverage other benefits.

Create a Server Action

Jump to this spot in the video  1:21 In your Next.js app, create a new async function to be your Server Action, and pass the form data to the function to get the image to upload. Note that image is the name assigned to the form input.

Convert the image to a buffer

Jump to this spot in the video  2:50 To upload the image to Cloudinary you need to turn the file into an array of bytes using an ArrayBuffer.

Upload the image to Cloudinary

Jump to this spot in the video  3:22 To upload the image to Cloudinary, use the Node.js SDK. Install and configure the Node.js SDK, then use the upload_stream method, wrapped in a promise, to upload the image.

Display the uploaded images

Jump to this spot in the video  4:43 The images that are displayed on the site are selected based on a tag. Ensure that the uploaded files have that same tag by adding it to the upload call. Then make sure to invalidate and refresh the page, using revalidatePath so the new images are displayed.

Keep learning

Related topics

If you like this, you might also like...

Upload Videos in Node.js
Upload videos to Cloudinary using the Node.js SDK
List images in Next.js
List images using the Next.js app router and Node.js SDK
AI Generative Fill using Next.js
Extend images using generative AI in a Next.js app

 

Cloudinary Academy

 

Check out the Cloudinary Academy for free self-paced Cloudinary courses on a variety of developer or DAM topics, or register for formal instructor-led courses, either virtual or on-site.

 

✔️ Feedback sent!

Rate this page: