Uploading files in NextJs 13 using Server Actions

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • In this video we'll look at handling file uploads using server actions in NextJs 13, we're going to use signed upload in Cloudinary as our cloud storage and React dropzone to allow drag an drop.
    👉🏼 The Ultimate NextJs Course
    🔗 www.hamedbahram.io/courses/ne...
    👉🏼 Source code (Github)
    🔗 github.com/HamedBahram/next-u...
    Chapters
    0:00 Intro
    1:50 Project setup
    3:00 Setting up dropzone
    6:20 Adding server actions
    17:12 Outro

Komentáře • 69

  • @mobinans8558
    @mobinans8558 Před rokem +3

    Thank you for this video, I'm so glad I stumbled upon this video! I've been searching all over for something like this. It's like you read my mind! The content is spot on and exactly what I needed. I appreciate the effort you put into creating this video. Keep up the fantastic work! Thanks once again.

    • @hamedbahram
      @hamedbahram  Před rokem

      Thanks, I'm glad you found it helpful.

  • @AlirezaMehrabiKali
    @AlirezaMehrabiKali Před 10 měsíci +3

    Hi Mr Bahram,
    I want to say I'm thankful and appreciate you, i always watch your videos. i follow you on linkedIn and youtube and follow your tutorials from Iran.

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      Thanks Alireza Jan, I'm glad to hear that.

  • @uixmat
    @uixmat Před 10 měsíci +1

    Just watched this through and it looks exactly like what i need to implement as a next step in my app. Looking forward to giving it a go!

    • @hamedbahram
      @hamedbahram  Před 10 měsíci +1

      There you have it! Glad to hear that.

    • @uixmat
      @uixmat Před 10 měsíci

      Followed part of the steps from the first video and went with the actions from this, working nicely - very easy to follow!@@hamedbahram

  • @LeonardoRossi-cr6fe
    @LeonardoRossi-cr6fe Před 11 měsíci +1

    Thank you for a making great videos, Hamed. A huge from Brazil

    • @hamedbahram
      @hamedbahram  Před 11 měsíci +1

      Thanks Leonardo! Welcome to the channel 🙂

  • @hqasmei
    @hqasmei Před rokem

    Great video Hamed! Thank you!

  • @niallmoore8240
    @niallmoore8240 Před 10 měsíci +1

    Thank you mate. This is so helpful!

  • @lonelyboy4033
    @lonelyboy4033 Před rokem

    you deserve way more subscribers !! Thank you so much!

  • @AndrulisTravel
    @AndrulisTravel Před 4 měsíci +1

    great content as always. Maybe you can make tutorial on how to upload multiple images with drag and drop functionality to Cloudflare image service. The service uses CDN, it's also has very friendly pricing compared to Cloudinary. Basically a form to submit a listing of some product with images and also using server actions :)

    • @hamedbahram
      @hamedbahram  Před 4 měsíci +1

      Thanks for the idea! I'll look into that.

  • @erkintek
    @erkintek Před 9 měsíci +5

    you should name this as "Uploading files to Cloudinary in NextJs 13".

    • @hamedbahram
      @hamedbahram  Před 9 měsíci

      Good point 🤔 Thanks for the suggestion.

  • @INteresting_2000
    @INteresting_2000 Před 10 měsíci +2

    you're a legend for not using typescript bro 😎

    • @hamedbahram
      @hamedbahram  Před 10 měsíci +1

      It's a mix; some of the videos are in TS but simple enough not to make the topic of the video more complex.

  • @BBocho
    @BBocho Před 5 měsíci +1

    Thank you very much!

  • @subodhbaral2350
    @subodhbaral2350 Před 11 měsíci +1

    Thanks Hamed

  • @investandcyclecheap4890
    @investandcyclecheap4890 Před 8 měsíci +1

    Thank you so much for this helpful guide. I would change the title to include "uploading files to Cloudinary". There are not a lot of tutorials that are covering this method in next.

    • @hamedbahram
      @hamedbahram  Před 8 měsíci +1

      Glad it helped! Thanks for the suggestion, that'd be more specific.

    • @investandcyclecheap4890
      @investandcyclecheap4890 Před 8 měsíci +1

      I was re-watching this video after learning about react-hook-form and I was wondering how youd do this with that library, specifically after calling RHF's handleSubmit(andYourSubmit FormActionFunction). It would be nice to add some other form inputs after your aweosme dropBox! Thanks! @@hamedbahram

  • @ThugLifeModafocah
    @ThugLifeModafocah Před 9 měsíci +1

    Nice touch about the signature. Thank you. What kind of account you created on cloudinary (there are the programmable media and digital asset management)?

    • @hamedbahram
      @hamedbahram  Před 9 měsíci +1

      Thanks. I'm on a paid plan that's based on number of transformations and uploads per month. The free tier is pretty generous.

  • @ReMoghadam
    @ReMoghadam Před 5 měsíci +1

    Dear Hamed, We are Proud of you, thank you for your great videos. How we can use only the part of React Drop Zone as a component and get files in Server Action to make our code Reusable in different forms ? I can't receive my files as FormData if I use React Drop Zone as a component as a child in its parent form and Server Action.

    • @hamedbahram
      @hamedbahram  Před 5 měsíci

      Thank you! I appreciate that. That's a good question, let me try that out.

  • @mojtabayam
    @mojtabayam Před rokem

    thanks a lottttttt

  • @abbaskareem5281
    @abbaskareem5281 Před rokem +1

    I like your way of teaching... You make hard topic easy to understand thank you very much sir .. One question please, If I have a backend endpoint (let say in django-rest-framework) which eccept formData I only need to make a post request to that endpoint no need to make sigture and all that staff ?

    • @hamedbahram
      @hamedbahram  Před rokem

      Thanks Abbas! Glad you find the content easy to understand. You can implement something similar here as well, whereby you would send the form data to your server action and let that handle everything instead of returning a signature to the front-end. I tried to show a typical flow in React apps but with use of server actions instead of an API route.

  • @sirpripyat4274
    @sirpripyat4274 Před 11 měsíci +1

    Thanks for the video, Hamed! Do u have some solution about fetching the url images from backend and showing them in front?

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      Do you mean after uploading them?

    • @sirpripyat4274
      @sirpripyat4274 Před 11 měsíci +1

      @@hamedbahram yess

    • @hamedbahram
      @hamedbahram  Před 11 měsíci +1

      @@sirpripyat4274 Once you upload them to your cloud storage - cloudinary in this example, you'll get a URL back which you can save to your database, you can use the same URLs to show the images using the `next/image` component. I'm not sure if this answers your question, but let me know if any part of this is unclear?

  • @musayazlk7654
    @musayazlk7654 Před rokem +1

    Hello. I don't want to use a 3rd party storage service like Cloudinary for images and videos. I keep video and image files under public with Multer, but in this case, images and videos uploaded later in production environment don't work. I want to keep the uploaded videos and images on my own server. How can I do this? Do I need to create a separate backend using Express or something similar?

    • @hamedbahram
      @hamedbahram  Před rokem

      Yes you would need to spin your own server, since the NextJs backend is deployed as server-less functions that don't live beyond after the execution.

    • @musayazlk7654
      @musayazlk7654 Před rokem +1

      @@hamedbahram Which structure do you use for image and video uploads? Are you creating a separate backend or are you using a 3rd party application?

    • @hamedbahram
      @hamedbahram  Před rokem +1

      @@musayazlk7654 I use cloudinary + google cloud storage for most of my meida storage.

  • @oluwafemifaleye6845
    @oluwafemifaleye6845 Před rokem +2

    Thank you for this interesting video❤, I have been waiting for it.
    What if I want to send the form data to an API route then handle the upload using multer or formidable. I have tried it countless time, but it seems not to work with the app router version. Maybe the multer library needs an update that will support the API routes in Next 13.4. Kindly tell me if I am wrong.
    Thank you

    • @hamedbahram
      @hamedbahram  Před rokem +1

      Well if you're using server actions, you won't need an API route handler, you can just perform any logic in your server actions. If you don't want to use server actions however, you can just handle form submission like we used to in regular React and then hit your own API endpoint with the data.

    • @oluwafemifaleye6845
      @oluwafemifaleye6845 Před rokem +1

      @@hamedbahram okay, thank you.

  • @liu-river
    @liu-river Před rokem +1

    This is super helpful. I have a question, so to upload multiple images, I tried to attach multiple files to the same form but that didn't work. It only works if I attach one photo at a time and post to the api. So to upload 3 photos, it would be 3 seperate calls to the api, ofc I can loop this, but is this the only way when using signed method?

    • @hamedbahram
      @hamedbahram  Před rokem +1

      I'm afraid so :) you have to loop and upload them one by one. Let me know if you figured to upload multiple together.

    • @liu-river
      @liu-river Před rokem +2

      ​@@hamedbahram From reading the doc there is multiple upload in node.js though, so maybe it's possible to build a next API route that receives files from the front-end as buffer and then batch upload using their SDK. But that's out of scope for this tutorial.

    • @hamedbahram
      @hamedbahram  Před rokem

      @@liu-river Beautiful! Thanks for sharing your findings Liu!

  • @yantaosong
    @yantaosong Před rokem +1

    hi , it is a great video , I have a doubt ,console.log is not working in action function ?

    • @hamedbahram
      @hamedbahram  Před rokem

      Hey, thank! Server actions run on the server, so you need to look at your terminal to see the `console.log()` result.

  • @caturbgs11
    @caturbgs11 Před 11 měsíci +1

    I have an issue where data doesn't get sent to the backend API when uploading a file that is larger than 5 MB.

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      Have you set any size restriction on the dropzone component?

  • @user-ny9em7sy7f
    @user-ny9em7sy7f Před 4 měsíci +1

    Do you know how to make it to work on mobile android and ios (upload file,i tried to add open and onClick: true but still not working...

    • @hamedbahram
      @hamedbahram  Před 4 měsíci

      Good question 🤔 should work in a similar way. I'd have to test it out.

  • @wakibtz7134
    @wakibtz7134 Před rokem +1

    Hi/ I have a problem. I when i use actions in client component i get an error that i can not use (use server) in client component. But i make all like you

    • @hamedbahram
      @hamedbahram  Před rokem +1

      Make sure you've added the `experimental` flag to your `next.config.js` and also keep in mind that you cannot create your server actions inside client components, you can create it in a separate file and import it in your client components. Let me know if this solves the problem?

  • @devangmanjramkar8393
    @devangmanjramkar8393 Před rokem +1

    Can you please make a video on how to upload file to an API which accepts only BLOB using formData field passed to the server actions please

    • @hamedbahram
      @hamedbahram  Před rokem

      That's interesting! I'll keep that in mind for future videos. Just to clarify, you want to upload a file to an external API using server actions?

  • @usamausman5225
    @usamausman5225 Před rokem

    Sir when you will launch your course I'm curiously waiting for that.

    • @hamedbahram
      @hamedbahram  Před rokem

      Some of the modules are done and available to start watching. I'm aiming to complete one module every week, and so you can continue learning gradually with me.

  • @thebenkalungi
    @thebenkalungi Před 8 měsíci +1

    How do you upload multiple files?

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      Oneway is to allow selecting multiple files and loop through them and upload them one by one.

  • @diwanshuji947
    @diwanshuji947 Před 2 měsíci

    Just Asking Can we also sign and upload In same server action is it possible or there is any caveat

  • @tseringlama2146
    @tseringlama2146 Před rokem +1

    hello brother please make a video on (express js) backend and (next js ) frontend about "server side " filtering data and pagination not using server action

    • @hamedbahram
      @hamedbahram  Před rokem

      Thanks for the suggestion. I'll keep that in mind for the upcoming videos.