File Upload in Next.js 13 App Directory with NO libraries! Client and React Server Components!

Ғылым және технология

Easily upload files and then use them server-side in Next.js 13! In the past, you would need to use third-party libraries to help with this, but NO MORE. See how you can upload files easily in client and server components.
📰 Newsletter 📰
Signup for my newsletter, "A Bit of SaaS," to keep you informed, inspired, and engaged with the latest trends, tips, and resources in the ever-evolving SaaS industry.
ethanmick.com/newsletter/
📜 Code 📜
• ethanmick.com/how-to-upload-a...
👨🏼‍💻 About Me 👨🏼‍💻
I am a principal software engineer and architect who loves building cool cloud software.
• Website: ethanmick.com/
• GitHub: github.com/ethanmick
• Twitter: / ethan_mick
• LinkedIn: / ethan-mick
• Agency: ethanmick.dev/
🔖 Chapters 🔖
00:00 - Intro
00:49 - Client Component
08:24 - React Server Component
11:23 - Outro

Пікірлер: 225

  • @alaricbell
    @alaricbell9 ай бұрын

    You are a life-saver. This is probably one of the very few channels that actually jumps in the raw coding part without any fancy library that most people don't care about. Thank you for the valuable informations!

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Heck ya. Gotta get straight to the good stuff.

  • @DorianDevelops
    @DorianDevelops7 ай бұрын

    You have no idea how helpful this was!!!! Thank you!

  • @SheriffKoder9
    @SheriffKoder96 ай бұрын

    Stunning, simple, short and to the point tutorial, i appreciate your approach towards being able to implement this functionality without adding more dependencies to the code. Bravo

  • @michaelmaher4317
    @michaelmaher43174 ай бұрын

    You're the best! We need more useful tutorials like this one. I guess most folks are just using libraries and demonstrating how to use them in their videos, but you did something really unique by showing a native approach that actually works. I'm truly thankful to you.

  • @samontefrankamirsong.5474
    @samontefrankamirsong.54747 ай бұрын

    You are a lifesaver man, I am trying next.js after react and react-native, I am not sure how to transition, you clearly gave me a path for me to follow. Thank you so much, you got a loyal subscriber here.

  • @miralirafiyev4646
    @miralirafiyev46468 ай бұрын

    Thank you for this tutorial Ethan. if someone is interested how to write file in to some folder inside the project const rootDir = process.cwd(); const path = join(rootDir, 'public', 'storage', file.name); it is going to put the uploaded file to project/public/storage/file.extension

  • @saputrauta09

    @saputrauta09

    6 ай бұрын

    thanks

  • @kev11n_eleven
    @kev11n_eleven7 ай бұрын

    Just what I was looking for - upload functionality without the need for any additional (probably costly) third party services. Thanks a million!

  • @CaseStudyQB
    @CaseStudyQB6 ай бұрын

    Thanks so much, I was looking for my solution for a day and this helped me get it! Much love

  • @mohammadbagheri6407
    @mohammadbagheri64077 ай бұрын

    Bro this saved my life in so many ways i can't thank you enough. I've been searching whole internert (in 3 languages) just to find out how i can save a file in nextjs api with app router and only this video worked for me.

  • @metasal
    @metasal6 ай бұрын

    Worked like a charm! Thank you

  • @cassioscabral
    @cassioscabral8 ай бұрын

    And Ethan saves the day again. Thanks for the videos

  • @Androb84
    @Androb84Ай бұрын

    Thanks!!! I've been looking for this!

  • @rogerangel9457
    @rogerangel94574 ай бұрын

    Your explanation delivery is awesome!

  • @yongjiang7832
    @yongjiang7832Ай бұрын

    Really helpful tutorial, after so much time spent on how to upload image in nextjs,I found this and it works so well...thanks a lot!

  • @harmony9169
    @harmony91698 ай бұрын

    Nice tutorial, it really help and also big props for going with vanilla option.

  • @wealthwiseNav
    @wealthwiseNav3 ай бұрын

    It was really awesome. You made it short and crisp

  • @manjarcode
    @manjarcode6 ай бұрын

    Thank you it really helped me, I was struggling with this problem the whole day :)

  • @mohammedmshal5483
    @mohammedmshal54838 ай бұрын

    This is amazing bro, love your lesson ❤❤

  • @vanjastojanovic5023
    @vanjastojanovic5023Ай бұрын

    Amazing tutorial, needed this part for my next :) project

  • @traezeeofor
    @traezeeofor3 ай бұрын

    Many thanks for all the same reasons shared by previous commenters. Tutors that focus on the basics for noob programmers have a special place in our hearts

  • @user-dh4py6qj2h
    @user-dh4py6qj2h4 ай бұрын

    Thank you very much! I was looking for a solution like this!

  • @alejandroz1606
    @alejandroz16065 ай бұрын

    Thanks a lot for this. This is the only resource I found about this two sides of the topic in the web.

  • @victorianguyen1288
    @victorianguyen128810 ай бұрын

    This is exactly what I'm looking for, thank you so much! 🙂

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Glad it was helpful!

  • @frackinfamous6126
    @frackinfamous612611 ай бұрын

    Love this one bro, super helpful!

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Thanks so much!

  • @TheHoinoel
    @TheHoinoel10 ай бұрын

    This is really clear and easy to follow. Thank you :)

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    You're very welcome!

  • @hironakae
    @hironakae11 ай бұрын

    Your video topics are always so relevant!

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    ❤️❤️

  • @cby1189
    @cby118910 ай бұрын

    You're a wizard, I've been looking everywhere for a solution like this

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Thank you! May the magic continue

  • @MrRhysSir
    @MrRhysSir5 ай бұрын

    thank you!! This helped so much

  • @work-eq8mr
    @work-eq8mr8 ай бұрын

    You're the best bro.... Thanks for you job!

  • @fagnersales532
    @fagnersales5329 ай бұрын

    Thank you so much. Both the video and blog post were very useful.

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Glad it helped! And thanks for the blog mention :D

  • @davidcrowell3041
    @davidcrowell30416 ай бұрын

    Thank you Thank you. Finally someone explained it.

  • @quantumrim3805
    @quantumrim380511 ай бұрын

    as usual very helpful, thank you

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Glad to hear that! Thanks!

  • @codezone135
    @codezone1356 ай бұрын

    You are a good man. Am suscribing right away

  • @anthonydiego007
    @anthonydiego0078 ай бұрын

    Thanks for the great video. Worked like a charm.

  • @ethan_mick

    @ethan_mick

    8 ай бұрын

    Glad it helped!

  • @FutureNewsDaily
    @FutureNewsDaily11 ай бұрын

    Just wanted to commend you on excellent content, choices and explanations!

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Thank you so much!

  • @Ali-ei3mg
    @Ali-ei3mg11 ай бұрын

    Thanks, man you are a lifesaver. This video helps me a lot

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Glad to hear that!

  • @huzaifaqayyum786
    @huzaifaqayyum78617 күн бұрын

    thanks, you saved me lot of time

  • @ankitzm
    @ankitzm5 ай бұрын

    awesome content 🔥

  • @nazrulhassan6310
    @nazrulhassan63109 ай бұрын

    this video is life saver i have been struggling with this video file upload in s3 .Thanks a lot

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Glad to hear that!

  • @willb.r2055
    @willb.r20559 ай бұрын

    this is amazing , i was struggling to find a source to learn this, , all i could find is usless libraries or old articles, your method is the easiest and most efficient, thank you very much

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    You're very welcome! Glad it was simple and easy :)

  • @westpoint7942
    @westpoint79423 ай бұрын

    thank you, man!

  • @albert21994
    @albert219949 ай бұрын

    insanely well done video!

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Thank you so much!

  • @dwolrdcojp
    @dwolrdcojp6 ай бұрын

    I think it should be mentioned that this is not a final solution for a production build if you're hosting on something like Vercel. You would need to use storage bucket solution like Vercel blob, supabase bucket, AWS, etc. Vercel will not let you write to their server because most of the time its trying to run these calls as serverless and there is no where to store it. Vercel offers a tmp directory where you could upload the file to with the serveless function but you have to immediately take it from the tmp directory and send it off to your storage bucket solution.

  • @andrewaj8417

    @andrewaj8417

    5 ай бұрын

    Any tutorials or links how to do it production ready properly? Thank you

  • @user-wr7ou8ih5s

    @user-wr7ou8ih5s

    5 ай бұрын

    But what if i don't wanna do it the old way , can i ?

  • @dwolrdcojp

    @dwolrdcojp

    5 ай бұрын

    @@andrewaj8417 I would just look at vercel blob or supabase storage bucket docs... you can call their API and easily pass your files for upload

  • @dwolrdcojp

    @dwolrdcojp

    5 ай бұрын

    @@user-wr7ou8ih5s Not sure what you mean by this? Maybe if your Next.js was self hosted on another host that supports directly writing to the same server your project is built and ran from. Vercel explicitly tells you that you cannot write files to their server. Not so much because a storage issue for them but the way they optimize for serverless

  • @user-wr7ou8ih5s

    @user-wr7ou8ih5s

    5 ай бұрын

    @dwolrdcojp i mean that im building a project for a company, and they don't wanna use external solutions like cloudinary or firebase to store images , so the only thing left for me is to save them as a long string in my db or inside the project folder 📂🤔 !and then i read your comment

  • @donewithwork
    @donewithwork7 ай бұрын

    great content :)

  • @amjadgul5074
    @amjadgul50745 ай бұрын

    You're a lifesaver. Unlike many other resources that dwell on unnecessary libraries, this channel dives straight into the essential coding aspects. Thanks for providing valuable information. Love from Pakistan

  • @xiiEJRAM
    @xiiEJRAM11 ай бұрын

    Amazing work and very well explanation

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Thank you! Cheers!

  • @0xtz_
    @0xtz_11 ай бұрын

    😮 this is sooooo helpful man thanks 🙏

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Any time! As always, thanks for your support!

  • @ApurboRoy7077
    @ApurboRoy70772 ай бұрын

    Thank you very Much.

  • @drums_in_my_head
    @drums_in_my_head11 ай бұрын

    This is what I've been looking last 3 days! Thank you a lot!

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Enjoy and you're welcome! Cheers!

  • @drums_in_my_head

    @drums_in_my_head

    11 ай бұрын

    @@ethan_mick unfortunately seems like it doesn't work when I deploy my project in Vercel. Does your project work there?

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    @@drums_in_my_head Hey, sorry, friend! This won't work on Vercel because Vercel's API route handlers are lamda functions with no permanent access to the filesystem. If you want to upload a file, you'll need to follow this guide *and then* send it to a blob file storage like AWS S3 or whatnot. This can get the contents of the file from the user. So you could do: const key = `${randomUUID()}${randomUUID()}`.replace(/-/g, '') const s3 = new AWS.S3() const params = { Bucket: bucket, Key: key, Body: buffer, } await s3.upload(params).promise() Where `buffer` is what you get from the request.

  • @toyinatofarati5512

    @toyinatofarati5512

    10 ай бұрын

    I tried it it mongodb but it didn't work any way out?@@ethan_mick

  • @VincentFulco
    @VincentFulco11 ай бұрын

    Great guidance. Kudos!

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Glad it was helpful!

  • @AlirezaMehrabiKali
    @AlirezaMehrabiKali10 ай бұрын

    great! you solved my problem. thank you

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Heck ya, glad I could help!

  • @user-he5uv6ij7e
    @user-he5uv6ij7e8 ай бұрын

    Thank you soooo much

  • @bestchoice7431
    @bestchoice74319 ай бұрын

    Just awesome. You have done a great job.

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Thank you so much 😀

  • @bestchoice7431

    @bestchoice7431

    9 ай бұрын

    @@ethan_mick I am stuck on the Vercel blob. Can you please make a video of the CRUD operation with Vercel blob?

  • @AjitJain-ym1cf
    @AjitJain-ym1cf10 ай бұрын

    Thanks, Man, This video helps me 💯

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Glad to hear it!

  • @eazybeeilori9911
    @eazybeeilori99118 ай бұрын

    You are a live saver

  • @taicheeeee
    @taicheeeee3 ай бұрын

    Bro...... You have no idea how appreciative I am. All the other tutorials about NextJS file uploading are using third party libraries but you.

  • @marceldavis3628
    @marceldavis36289 ай бұрын

    Thank you man. I like that its the cleanest and simplest solution. I tryed to do the same with formidable and other libarys, but i alsways got stuck, because of bugs with next js and blobs. I wasted probably 2 days on that.

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Yeah, using libraries can be necessary for some of the more complex use cases, but this is the simplest way by far. And it should get you pretty far!

  • @rodionkovalskii3015
    @rodionkovalskii30158 ай бұрын

    Thank you!

  • @aidanbraski
    @aidanbraski8 ай бұрын

    thank you ethan

  • @Salam-jt7vu
    @Salam-jt7vu9 ай бұрын

    Awesome man you're awesome

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Thanks :D

  • @user-cp6cq8cx6m
    @user-cp6cq8cx6mАй бұрын

    Thanks!!!!!

  • @jessequartey
    @jessequartey11 ай бұрын

    Funny how i needed this in a project i am working on and it presented itself. Ctrl+C & Crtl+V lets go

  • @ethan_mick

    @ethan_mick

    11 ай бұрын

    Let's goooo!

  • @Mayank-qk6ui
    @Mayank-qk6ui3 ай бұрын

    Thanks bro

  • @mohdsahil226
    @mohdsahil2268 ай бұрын

    Really great video. Thanks! Would you please tell me how to upload multiple files?

  • @shivamanand8194
    @shivamanand81948 ай бұрын

    After using this method to upload image, when we go to host website on versal then it give error

  • @souvikkarmakar9646
    @souvikkarmakar96463 ай бұрын

    thank you💗💗💗💗💗💗💗💗

  • @franklinmayoyo
    @franklinmayoyo8 ай бұрын

    You don't know how much you've saved me😭 Thank you💌

  • @cbejjani
    @cbejjaniАй бұрын

    Thank you for your amazing video 🔥🔥🔥 I have a question : how to upload a file using nextjs structure /pages/api/upload.js into Laravel endpoint

  • @pthiago_s5075
    @pthiago_s50754 ай бұрын

    6:40 this part saved my project

  • @omotayosamson6892
    @omotayosamson68924 ай бұрын

    Nice video ethan. This video is super informative. I tried to kick it up a notch by trying to add text to the inage using the same logic, but it doesn't work, any tips on how to do that. Thanks in advance

  • @jeankerby6908
    @jeankerby69088 ай бұрын

    Two words, Thank you.

  • @ethan_mick

    @ethan_mick

    8 ай бұрын

    My pleasure :)

  • @programmingwithnit5308
    @programmingwithnit530810 ай бұрын

    Thank you.

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    You're welcome!

  • @zakriarehan2379
    @zakriarehan23798 ай бұрын

    It has a limit of uploading 1mb file only. How to resolve connection reset error for large file in case of api route? Thanks,

  • @yousuf4you
    @yousuf4you3 ай бұрын

    Sir, Does it work after the production build???

  • @user-es4fg5sc3s
    @user-es4fg5sc3s8 ай бұрын

    Hii, How do you specify File size-limit to an api enpoint in Nextjs 13.5, I am getting "413 Payload too large" for pdfs which are 3 MB but at the same time images are getting uploaded of same size

  • @mohdsahil226
    @mohdsahil22610 ай бұрын

    Great! Thank you! It helped alot. Would you please also explain how to display file when select on the client side before uploading?

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Thanks! I can make a vid about this, but in short you'd use the File Reader API (developer.mozilla.org/en-US/docs/Web/API/FileReader) to read the contents of the file once the user has selected it, turn it into a blob, and display that as an IMG tag.

  • @iamjustlearner5866
    @iamjustlearner58666 ай бұрын

    Using this method is good if we uploading from front-end, but it has a problem when we want to test from postman, we cannot use form-data type file because in the backend only accept string type (buffer of the image). Is there any alternative so we can send the file to the backend without create buffer first?

  • @user-es2jt6qe2o
    @user-es2jt6qe2o6 ай бұрын

    i did something similiar to what you did. that was great in development phase but in production mode, the newly uploaded image cannot be found by next js. any help my man?

  • @ullaskunder
    @ullaskunder7 ай бұрын

    I tried this just for testing no 3rd party or any thing but I send that buffer as a response and tried to pass it to the Next it worked but I tried sending larger image like more then 1mb why tag does not render it, is it to heavy ?

  • @felipeadeildo
    @felipeadeildo6 ай бұрын

    And in Next Js 14? The Next JS 14, the "NextApiRequest" type does not have formData property..., so how do i do?

  • @raybenchen
    @raybenchen10 ай бұрын

    This works pretty much only to app/api route approach and the API handler name needs to be POST() to avoid "Request.formData is not a function" error. The old page/api won't work (could be a bug, but Next JS documentation doesn't say)

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Yeah, this is in the App directory only. Since that's the new default and recommended approach I only covered it instead of the pages directory.

  • @cesarayalavargas3623
    @cesarayalavargas36234 ай бұрын

    Can this be done with server actions?

  • 4 ай бұрын

    Literalmente copié todo el código tal cual en mi proyecto y nada. Me da un error TypeError: s is not a function. Nada, para mí es imposible ya...

  • @user-jh4fn5np1q
    @user-jh4fn5np1q10 ай бұрын

    Thanks bro :>

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    💪

  • @alejandroz1606
    @alejandroz16065 ай бұрын

    How to handle dynamic directories? getting error when the directory does not exist and need to create a new one

  • @Alex.Shalda
    @Alex.ShaldaАй бұрын

    great video, I can upload, but I have an e-commerce, how do I serve the images with the products?

  • @ahmedhassaballa9909
    @ahmedhassaballa990910 ай бұрын

    Hi, thanks for your great explanation, i follow the session till the end , i want save the uploaded file in Azure blob storage, so for POST function i need to override the node fs and replace it with blob component? just asking how can i approach it. thank you

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Yes, correct! Instead of saving it as a file, use the Azure Library to upload the buffer to Azure.

  • @hydrognspa8753
    @hydrognspa875310 ай бұрын

    OMG thanks a lot! This video saved me. All previous tutorials I found used more complex methods and additional packages, and I really don't want to add more dependencies to my project ^^' Just a question. Does this work the same way for multiple files? I guess the type would be instead

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Yep, you've got it! When reading in the form data it'll be `File[]` and then you can iterate over the array of files.

  • @user-py8ef3de6u
    @user-py8ef3de6u10 ай бұрын

    Thank you for your great work , I am trying to do your solution in JS not TS but this line was error const data = await request.formData() request.formData() is not a function . if you know why please help me

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Make sure you’re on the latest version of Next.js and that this is in the app dir, the pages directory handles this differently.

  • @sagarjani8760
    @sagarjani87607 ай бұрын

    While trying to upload the file, API throws 413 -large metadata, also the app is deployed on Vercel.

  • @sergeishmelev8000
    @sergeishmelev80005 ай бұрын

    This was very useful, but is there any way to delete a file without using NextJs libraries?

  • @Gcastanheira
    @Gcastanheira8 ай бұрын

    obrigado gringão! vc é o cara

  • @hamedkhodadadi3607
    @hamedkhodadadi36079 ай бұрын

    very good

  • @ethan_mick

    @ethan_mick

    9 ай бұрын

    Thank you! Cheers!

  • @SemGabelko
    @SemGabelko6 ай бұрын

    This doesn't work with pages routing. formData is not a function. Anyone found a solution to that?

  • @cr7johnChan
    @cr7johnChan10 ай бұрын

    Hey bro can u pleas show us on how to upload files from nextjs to aws s 3 bucket

  • @ethan_mick

    @ethan_mick

    10 ай бұрын

    Heck ya I can

  • @billma1516
    @billma151619 күн бұрын

    You save a file outside the Nextjs folder, How can Nextjs read it?

  • @longsyee
    @longsyeeАй бұрын

    i wish you can do a tutorial on javascript too since im using javascript instead of ts while building T_T is a hell for me doing file upload for local. But above video is great using TS. i think i should get to TS learning

  • @AIZEN155
    @AIZEN1558 ай бұрын

    When you say file , do you mean video and image and pdf and every file , or just a image

  • @ilikethemost
    @ilikethemostАй бұрын

    found whole internet but not getting solution of this | is there any alternative of this method : (node:23124) ExperimentalWarning: buffer.File is an experimental feature and might change at any time

  • @julstaps
    @julstaps8 ай бұрын

    Thank you! npm run start cannot read the image or video in public folder. Is it possible to store and dynamically read the file? the path is saved to database. I need to restart the run to able to read the image.

  • @Ta9i

    @Ta9i

    3 ай бұрын

    so did you find a fix for this probleme

Келесі