Upload Files with Fetch - JavaScript Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ย. 2024
  • In this video we'll be taking a look at how to upload files using the Fetch API - this works by using the FormData object within JavaScript.
    Support me on Patreon:
    / dcode - with enough funding I plan to develop a website of some sort with a new developer experience!
    For your reference, check this out:
    developer.mozi...
    Follow me on Twitter @dcode!
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

ความคิดเห็น • 72

  • @皓-b1m
    @皓-b1m 2 หลายเดือนก่อน

    best and simplest upload js script forever.

  • @HugoGuzmanC
    @HugoGuzmanC 4 ปีที่แล้ว +8

    Thank you for sharing your knowledge!

  • @VictorSantos-yb8ir
    @VictorSantos-yb8ir 2 ปีที่แล้ว +2

    Thank you very much, you helped me a lot

  • @kashanahmad8014
    @kashanahmad8014 4 ปีที่แล้ว +1

    thanks mate! You solved one heck of a headache for me!

  • @romimaximus
    @romimaximus 5 ปีที่แล้ว +3

    Very well explained

  • @shantanuchhailkar3437
    @shantanuchhailkar3437 2 ปีที่แล้ว

    Thanks man!! This really working and solved my issue in a minute.

  • @nigelpallatt
    @nigelpallatt 3 ปีที่แล้ว +5

    HTTP/1.1 405 Method Not allowed ???

    • @ManpreetSingh-cv4it
      @ManpreetSingh-cv4it 6 หลายเดือนก่อน

      Hey, i am getting the same issue, were you able to resolve it?

  • @jasonlewis6805
    @jasonlewis6805 3 ปีที่แล้ว +1

    Great post, clear and to the point. Just what I needed!

  • @reidliberato8917
    @reidliberato8917 2 ปีที่แล้ว

    this is so simple i actually feel like its cheating. Thanks for this

  • @thebewerter5730
    @thebewerter5730 3 ปีที่แล้ว

    this helped me a lot, thanks for sharing! cheers!

  • @raveg9413
    @raveg9413 4 ปีที่แล้ว +2

    it's not working... I write this code just same as you are and set same directory, but I can't received image file in upload, cause they didn't send.

  • @ikurennyi
    @ikurennyi 5 ปีที่แล้ว +3

    Hello! May I ask you which VS Code them are you using in this video? Looks very nice!

    • @dcode-software
      @dcode-software  5 ปีที่แล้ว +3

      I'm using Roboto Mono as the font and the theme is Min Dark.

  • @BillTheChill654
    @BillTheChill654 2 ปีที่แล้ว +2

    I copied this code exactly as written and got a 405 error. Any idea what the problem could be?

    • @Hamza_n
      @Hamza_n 10 หลายเดือนก่อน

      Did you figure it out?

  • @AshwinSKumar
    @AshwinSKumar ปีที่แล้ว +1

    How would you create it so that it automatically chooses a certain file type?

  • @vishnuvardhan8686
    @vishnuvardhan8686 4 ปีที่แล้ว +2

    Hello! When I follow the same image not copied or saved or uploaded into destination folder.

  • @columbiantoad
    @columbiantoad 2 ปีที่แล้ว +1

    I want to use multiple files in my project. Would I just change the line "formData.append("inpFile", inpFile.files[0])" to "formData.append("inpFile", inpFile.files)"? Then on the PHP file, make a for loop which would go through the files indexes?
    $File = $_FILES['inpFile'];
    For example: $FileSize = filesize($File['name'][$i]); // $i = the index of the for loop.

  • @bjarnieinarsson3472
    @bjarnieinarsson3472 3 ปีที่แล้ว +2

    I would have at least mentioned the most important missing part.. the php file it self.

    • @dcode-software
      @dcode-software  3 ปีที่แล้ว +5

      Unfortunately not because this video is about the front end, using JavaScript. It will work for all server side technologies

  • @davidfont2596
    @davidfont2596 3 ปีที่แล้ว +1

    When I press the "upload file" button @6:00 I get "Uncaught ReferenceError: can't access lexical declaration 'formData' before initialization index.html:24EventListener.handleEvent*"

  • @TheIguana3d
    @TheIguana3d 4 ปีที่แล้ว

    Thanks man for the video!!!!

  • @divinechukwu505
    @divinechukwu505 2 ปีที่แล้ว

    Thank You For This

  • @gracialonignasiver6302
    @gracialonignasiver6302 2 ปีที่แล้ว +2

    This posts images perfectly, but does anyone else get a weird error where no other code will run inside the event besides the actual fetch post?

  • @atouchofa.d.d.5852
    @atouchofa.d.d.5852 2 ปีที่แล้ว

    Beautiful man

  • @goncalomarques7022
    @goncalomarques7022 3 ปีที่แล้ว +2

    bro i got 405 method not allowed can u give some help

    • @goncalomarques7022
      @goncalomarques7022 3 ปีที่แล้ว +1

      if someone is getting the same problem u just need to open it as a server and not as localhost th-cam.com/video/ohkMRA74MB4/w-d-xo.html see that video use xamp and open 80 port and it should work

  • @mohamedshahrul1750
    @mohamedshahrul1750 ปีที่แล้ว

    please answer this... how does file is sent to server without converting to base64? how does we send it in binary format? doesn't http only support text? thats why we convert it into base64 string format right? pleaseeee explain me...

  • @trusabaka9747
    @trusabaka9747 ปีที่แล้ว

    What about uploading BIG files? Making chunks, etc.?

  • @MichaelShingo
    @MichaelShingo ปีที่แล้ว

    I get a 500 Internal Server Error when I click submit and it doesn't upload the file to the folder :(

    • @MichaelShingo
      @MichaelShingo ปีที่แล้ว

      No error now that I installed PHP from homebrew and told VScode where the PHP executables are...now still nothing in uploads, looks like my $_FILES array is null

  • @beyondcraziness8847
    @beyondcraziness8847 3 ปีที่แล้ว

    I want to upload a file to AWS S3 bucket... Can I pass the URL instead of upload.php file location... If not what else can I do can you please tell me?

  • @fredfromearth7263
    @fredfromearth7263 2 ปีที่แล้ว

    OK but then where is it the file on the server? cannot find it. Good question, right?

  • @Cubas1111
    @Cubas1111 ปีที่แล้ว

    Nice video! I'm working with Django and I get this error ""415 (Unsupported Media Type)"" I already googled as much as i can and tried different code but without success. Any advice please (?)

  • @parabunx
    @parabunx 2 ปีที่แล้ว

    can we code the upload.php in JS ?? Because i code on shopify and shopify don't accept PHP....

  • @caprioficial
    @caprioficial ปีที่แล้ว

    thanks to you!

  • @kirito6374
    @kirito6374 2 ปีที่แล้ว

    Which server are you using?

  • @dijiflex
    @dijiflex 4 ปีที่แล้ว

    So if i have the name attribute on on the input tag, does it mean i can now avoid using the formdata.append() ?

  • @juliolh06
    @juliolh06 ปีที่แล้ว

    excelente

  • @ינוןאלבז-כ1ז
    @ינוןאלבז-כ1ז 3 ปีที่แล้ว

    thanks

  • @Brunocriacoes
    @Brunocriacoes 3 ปีที่แล้ว

    good job :)

  • @dastgirakhtar9782
    @dastgirakhtar9782 ปีที่แล้ว

    100 working 👏👏👏❤❤

  • @joseraimundofernandesfilho7570
    @joseraimundofernandesfilho7570 5 ปีที่แล้ว +1

    Hello dcode I really liked ur video. May I ask you how can I make the code version of upload.php in nodejs?

    • @dcode-software
      @dcode-software  5 ปีที่แล้ว +1

      You can use Express and Multer :)

    • @joseraimundofernandesfilho7570
      @joseraimundofernandesfilho7570 5 ปีที่แล้ว

      ​@@dcode-software Hello dcode. I tried with angular + Express and connect-multiparty and its working but the file name is becoming a hash like: 2wrty267yw2.pdf. Do you know how can I fix that?

  • @okancingoz
    @okancingoz ปีที่แล้ว

    How can i use that upload file path in javascript?

  • @paoro814
    @paoro814 3 ปีที่แล้ว

    will the php file run even with xammp or wampp server?

  • @webdev4786
    @webdev4786 3 ปีที่แล้ว

    perfect

  • @sidnaesk295
    @sidnaesk295 2 ปีที่แล้ว

    Love you :-)

  • @louisaldorio7251
    @louisaldorio7251 4 ปีที่แล้ว +2

    why we must use php again?

  • @mohammedalzamil7191
    @mohammedalzamil7191 4 ปีที่แล้ว

    awesome

  • @sandortudja2087
    @sandortudja2087 3 ปีที่แล้ว

    It is not working for me.
    I can check in the chrome developer tool the FormData is there as binary file
    But the selected picture do not in the uploads directory for some reason.
    Anyone have any idea what is the reason of this?

    • @sandortudja2087
      @sandortudja2087 3 ปีที่แล้ว

      Ok never mind I figured it out.
      There was some permission issue when the php file tried to write.

  • @mushfiqussalahin6930
    @mushfiqussalahin6930 4 ปีที่แล้ว

    I get 404. if u could guide me what am I doing wrong

  • @woofelator
    @woofelator 4 ปีที่แล้ว

    I'm getting no errors, and no file is showing up in my uploads folder.

    • @ConsulthinkProgrammer
      @ConsulthinkProgrammer 3 ปีที่แล้ว

      Try an alternative with apps script
      th-cam.com/video/m3Sc7nVLPP8/w-d-xo.html

  • @anthonymorell692
    @anthonymorell692 4 ปีที่แล้ว

    Has anyone implemented this with django backend? Everything follows along accordingly up until setting up the fetch procedure. I have problems 1.) getting the CSRF token and 2.) keep getting a 302 code. Any advice is greatly appreciated!

    • @ConsulthinkProgrammer
      @ConsulthinkProgrammer 3 ปีที่แล้ว

      Try an alternative with google apps script
      th-cam.com/video/m3Sc7nVLPP8/w-d-xo.html

  • @bikramchettri9405
    @bikramchettri9405 5 ปีที่แล้ว

    Can you teach how to validate files using javascript?

    • @dcode-software
      @dcode-software  5 ปีที่แล้ว +2

      I'll be doing a video soon on the File API :)

    • @bikramchettri9405
      @bikramchettri9405 5 ปีที่แล้ว

      @@dcode-software Thank buddy.

  • @AdriiPing
    @AdriiPing 4 ปีที่แล้ว

    Mine is getting error 400 : body must contain a valid content

    • @dcode-software
      @dcode-software  4 ปีที่แล้ว

      Try making sure your content type is correct