From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Implementing image storage functionality

Implementing image storage functionality

In this lecture, we will see how we can store images into the server. So let me open the VS code and navigate to the product action.js file. Now let's take one variable called file and assign the images value by writing const file is equal to form data dot get image. Let's create one variable for imagePath like let imagePath is equal to blank. Before I go further, I will check if the file is null or undefined by writing if file. Now inside the if condition, we will create a variable to read the file's content and convert it into an array buffer asynchronously. So I will write const bytes is equal to await file.arrayBuffer. To use the bytes, we need to convert it to a buffer. So I will say const buffer is equal to buffer.from bytes. Now I want to store all the images into a folder called uploads which should be in the public directory. And for that, let's create one variable called UploadDir outside the create product. So I will say const Upload underscore dir is equal to. Here we…

Contents