Last updated: Sep-12-2024
On this page:
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
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
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
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
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
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
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
- Find all upload options in the Upload API reference.
- See what's possible with the Cloudinary Next.js SDK.
- Watch more Dev Hints videos on the Cloudinary YouTube channel.
If you like this, you might also like...
Upload Videos in Node.js
Upload videos to Cloudinary using the Node.js SDKList images in Next.js
List images using the Next.js app router and Node.js SDKAI Generative Fill using Next.js
Extend images using generative AI in a Next.js app
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.
✖️