Это видео недоступно.
Сожалеем об этом.

Previewing Image Before File Upload On Websites - HTML, CSS & JavaScript Tutorial

Поделиться
HTML-код
  • Опубликовано: 14 авг 2024
  • In this video I'll be demonstrating how it's possible to use some simple JavaScript in order to display a preview of an image before uploading it to the web server.
    Learn more about Data URLs here:
    • What are Data URLs? - ...
    Support me on Patreon:
    / dcode - with enough funding I plan to develop a website of some sort with a new developer experience!
    Follow me on Twitter @dcodeyt!
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
    #dcode #javascript #css

Комментарии • 161

  • @dcode-software
    @dcode-software  3 года назад +5

    If you want to learn more about how Data URLs work, see my quick overview video below 👇
    ruclips.net/video/ipoLqQCEQMs/видео.html

  • @ashraftaha365
    @ashraftaha365 4 года назад +4

    Great! This is simply the best tutorial regarding file uploading with preview using pure JavaScript. Good job.

  • @withinbracket7830
    @withinbracket7830 3 года назад

    Trust me this is the best tutorial channel I ever seen

  • @sukmana5001
    @sukmana5001 2 года назад +1

    Best tutorial everr...

  • @samsar7
    @samsar7 4 года назад +7

    thank you so much. may Allah bless you . very useful tutorial.

  • @peshotod
    @peshotod 2 года назад

    Yes found it, sorry mistyping.All work now thank you Dom.

    • @adityabiswas7912
      @adityabiswas7912 2 года назад

      how did you fix it may I ask? I'm having the same issue, It say's it's something to do query selector being null or something and I cannot find the issue.

  • @SkyVergil83
    @SkyVergil83 3 года назад +1

    Just a question, why reader.readAsDataURL(file) needs to be after reader.eventlistener( ) function ???

  • @pranavshete7010
    @pranavshete7010 3 года назад +1

    Thank you so much bro❤️ I just did it in my project 👍👍Thanks a lot

  • @Daniel-xt9lm
    @Daniel-xt9lm 2 года назад +5

    8:14 - Don't use arrow functions, they inherit their "this" from the parent scope

  • @TulioSandoval
    @TulioSandoval 3 года назад

    Bro, this helped me a lot, but a friend came with the following solution: img.src = URL.createObjectURL(file). Works like a charm!

  • @nehalsingh785
    @nehalsingh785 2 года назад

    awesome tutorial sir

  • @AngelixGamer
    @AngelixGamer 3 года назад +1

    Very good video, you earned one more subscriber!

  • @telhernandez2347
    @telhernandez2347 3 года назад

    Thank you soooo muchhh!☺. I just subscribed.

  • @Timo75322
    @Timo75322 4 года назад +5

    how do i solve the error it shows in the log "previewContainer is undefined"

  • @arjunvenugopal4763
    @arjunvenugopal4763 4 года назад +2

    You just earned one more subscriber!

  • @pawpaaj
    @pawpaaj 5 лет назад +1

    Nice trick with these null values, didn't know about that. Thanks a lot!

  • @rickyu1978
    @rickyu1978 3 года назад +1

    Why not use createObjectURL(file) which will create a blob reference to the file in memory vs creating a base64 string ? which one is more performance. createobjecturl is easier one line no callback needed.

  • @de-ke4xz
    @de-ke4xz 3 года назад

    you know i was struggiling to find to preview image when i found you it became
    easy

  • @kopvi46
    @kopvi46 2 года назад

    u are the best dude, thank u

  • @fabsse-bx8on
    @fabsse-bx8on 4 года назад +2

    what about previewing multiple images? what changes should i do while implementing ur code?

  • @medg2772
    @medg2772 3 года назад

    Thank you Brother ! just amazing as the rest of your tutorials

  • @teerapongruplek8509
    @teerapongruplek8509 4 года назад +1

    It's great tutorial! I would like you make tutorial about showing preview image with remove button and upload multiple files.
    thank you for all tutorial from you. It's very nice.

  • @raffy7384
    @raffy7384 4 года назад

    thank you for this tutorial. Very clear and easy to follow.

  • @911_dady
    @911_dady 4 года назад +1

    Pure Content . Respect 💛💛

  • @edgartoon1
    @edgartoon1 4 года назад +2

    Very good tutorial, I just have a question, do you know how to do that but having 2 input files in the same form without repeating the function 2 times?

  • @peshotod
    @peshotod 2 года назад

    Unfortunately for some reason keep giving me error.excel.js:38 Uncaught TypeError: Cannot read properties of null (reading 'style')
    at HTMLInputElement. (excel.js:38:24)
    previewDefaultText.style.display = "none";
    I am doing something wrong but don't know what.Source code would be helpful.

  • @osmanyazici6032
    @osmanyazici6032 3 года назад

    Çok güzel bir video bende abone oldum. anlatımınız ve çözüm çok harika. Çok teşekkür ederim.

  • @beksultanomirzak9803
    @beksultanomirzak9803 3 года назад

    wow Good Tutorial Sir

  • @andrewbaillie2475
    @andrewbaillie2475 2 года назад

    Brilliant tutorial, explained clearly and comprehensively.
    How would one save the image after it was uploaded, ie if we changed the image size?
    Thank you in advance.

  • @OrgesKreka
    @OrgesKreka 4 года назад +4

    The keyboard sound is making the video sound really good. Can you share your keyboard model ?

    • @dcode-software
      @dcode-software  4 года назад +3

      CoolerMaster MasterKeys Pro S with Cherry MX Blue

    • @dheerajn3314
      @dheerajn3314 3 года назад

      Why its not working while I have written same code as video in html fiile... Where is missing...
      And tell me about links before head tag... Should I link any other js, css... Files to this file... Help..

  • @sachinmags9605
    @sachinmags9605 4 года назад

    great tutorial man!! keep up the good work

  • @Nicole-se7zj
    @Nicole-se7zj 4 года назад

    Finally got this working! Thank you!

  • @solomondanso1034
    @solomondanso1034 4 года назад +1

    Nice tutorial sir, but can you help on how to preview multiple selected images before upload? Like airbnb have on their website

  • @LibraryOfTheOligarchs
    @LibraryOfTheOligarchs 9 месяцев назад

    awesome! Thank you!

  • @Kylegil00
    @Kylegil00 3 года назад

    Great Tutorial

  • @emilyandrews7924
    @emilyandrews7924 3 года назад +2

    Would love to see an image previewer tutorial that handles multiple files. I can find anything anywhere

    • @davidquintero7692
      @davidquintero7692 2 года назад

      hey! too late, but you can try making the containers that you need with the same classnames an id's, and in JS just querySelectorAll, like this (more or less):
      inpFile.addEventListener('change',function (){
      for(let i = 0; i < this.files.length; i++){
      const file = this.files[i]
      if(file){
      const reader = new FileReader()
      previewDefaultText[i].style.display = "none"
      previewImage[i].style.display = "block"
      previewImage[i].style.objectFit = "cover"
      reader.addEventListener('load', function(){
      console.log(this)
      previewImage[i].setAttribute("src", this.result)
      })
      reader.readAsDataURL(file)
      }
      }
      })
      hope help you ! (sorry for my english)

  • @djebbarabderrahmene1813
    @djebbarabderrahmene1813 2 года назад

    you are the best

  • @anghungthucanghungthuc2265
    @anghungthucanghungthuc2265 3 года назад

    Great tutorial, wish you more success.

    • @dheerajn3314
      @dheerajn3314 3 года назад

      Why its not working while I have written same code as video in html fiile... Where is missing...
      And tell me about links before head tag... Should I link any other js, css... Files to this file... Help..

  • @varunrajkumar2764
    @varunrajkumar2764 3 года назад

    Nice tutorial, this helped a lot.

  • @sagarkumar-ns3gr
    @sagarkumar-ns3gr 4 года назад +1

    thanks a lot you helped me to solve my problem

  • @borisbosnjak9772
    @borisbosnjak9772 5 лет назад

    Never worked with this, great tutorial!

  • @midhirrana2646
    @midhirrana2646 2 года назад

    Thanks, it helped.

  • @samuelreyes1835
    @samuelreyes1835 3 года назад

    Algún día programare así como tu amigo!

  • @bikramchettri9405
    @bikramchettri9405 5 лет назад +2

    Thanks for this will surely watch this tutorial. Do you have any idea on how to make a link previewer?

    • @tyfoodsforthought
      @tyfoodsforthought 4 года назад

      Ultimately, I think you have to use/create a webscraper. Alternatively, you can use a link previewer API that already have this functionality implemented. I use Facebooks Link Preview API.

  • @JacoblBroughton
    @JacoblBroughton 4 года назад +1

    Awesome tutorial!

  • @bartoszoch1201
    @bartoszoch1201 4 года назад

    I have error Cannot read property 'style' of null in your file it is line 51-52 and 61-62. Can you help me?

  • @skyrider53
    @skyrider53 3 года назад

    Just 1 question though. The css link with href /assets/docde.css where is the actual css? Like is it a code I have to code myself or that is on the internet, what?

  • @fnoorman
    @fnoorman 3 года назад

    good work...just suscribed to u..

  • @huntmast
    @huntmast Год назад

    nice work

  • @chainenaimslama1870
    @chainenaimslama1870 Год назад

    thx a lot and very good

  • @thrang_eo
    @thrang_eo 4 года назад +1

    Finally !!! I got it thank you sir

    • @dcode-software
      @dcode-software  4 года назад

      No worries mate

    • @thrang_eo
      @thrang_eo 4 года назад

      Yo sir please answer this. The file path is too Long. How do I make it short?

    • @sourabmajhi8755
      @sourabmajhi8755 4 года назад

      hello

  • @dheerajn3314
    @dheerajn3314 3 года назад

    Why its not working while I have written same code as video in html fiile... Where is missing...
    And tell me about links before head tag... Should I link any other js, css... Files to this file... Help..

  • @thirupathid8231
    @thirupathid8231 4 года назад

    Nice job. Thanks man

  • @j4ck3
    @j4ck3 3 года назад

    Uncaught TypeError: Cannot read property 'style' of null
    the "image preview" text goes away but no image is loading

  • @nestorhenriquez1321
    @nestorhenriquez1321 3 года назад

    i don't understand, where the javascript is in the same file but i see that there are .js files that contain only javascript.
    what's the difference between those two?

  • @pascaltourneur9202
    @pascaltourneur9202 4 года назад

    Hi. Great Thanks. But I have Problems with the fix-orientation.. what can I do?

  • @sbmahapatra
    @sbmahapatra 2 года назад

    How to save the file from the drop box window? I mean to say how to save the file into your local machine?

  • @TheSpiralnotizblock
    @TheSpiralnotizblock 3 года назад

    such a nice tutorial. Cool!

  • @hancymtenga7182
    @hancymtenga7182 3 года назад

    Thank you so much for the awesome video.. I am kindly asking could you do a QNS drag where a user drags a series of disordered options in the correct order.

  • @tgt106
    @tgt106 3 года назад

    Is there a way that we can preview files (pdf,excel) that we choose in file selector, in a different browser tab?

  • @vircycure
    @vircycure 4 года назад

    Very helpful Video

  • @vulembereinfos9527
    @vulembereinfos9527 3 года назад

    Merci pour tuto ca m'as beacoup aider

  • @kamirty
    @kamirty 3 года назад

    لم ينجح معي ؟
    Input File Upload img file +html5 canvas?
    ببرنامج
    abode animate

  • @stephtech1021
    @stephtech1021 4 года назад

    hello can u do it with and svg image when you click onthe svg it automatically carry to where you have the picture to upload

  • @niyan1873
    @niyan1873 3 года назад

    Love you a ton brother

  • @AntNZ
    @AntNZ 3 года назад

    Hey mate, just wondered if you can do a tutorial on getting the exir data extracted from the image using js, before upload? Been trying to do this with node js packages including exfir but am not having any success getting the gps coords extracted. I get all other data bit no gps coords. Any help would be awesome!

  • @joseavalos2033
    @joseavalos2033 4 года назад

    amazing!! Thanks a lot!!

  • @ConsulthinkProgrammer
    @ConsulthinkProgrammer 3 года назад

    Thanks Sir. Do you know how to reduce that file size (suppose from 5 MB to 1 MB) ?

  • @shivammahobiya7981
    @shivammahobiya7981 4 года назад

    Wow! You doing great...

  • @envplayz
    @envplayz 3 года назад

    what i want to do is when user inserts image get the image as URL and make it a variable plz help

  • @jadersbr
    @jadersbr 4 года назад

    fantastic, tks man

  • @zikaruus3693
    @zikaruus3693 4 года назад

    The picture gets swapped into a horizontal position, the photo itself is vertical. Any idea how to fix this? :(

  • @VamsiKrishna-rs1sd
    @VamsiKrishna-rs1sd 3 года назад

    Need the same functionality using lwc. If anyone have a blog plese share me.

  • @busgi
    @busgi 4 года назад

    Hey in the line of
    previewDefaultText.style.display="none";
    i get this error message.
    Cannot read property 'style' of null
    at HTMLInputElement. (home?inpFile=:64)

    • @BluecreeperXD
      @BluecreeperXD 4 года назад

      i see the same error, you found a awsner?

    • @Rickey13
      @Rickey13 3 года назад

      i got the same issue, i corrected it by including the css at the beginning of the video. Hope this works :)

  • @user-fg6un4ho9z
    @user-fg6un4ho9z 5 лет назад +1

    previewImage.setAttribute("src", "") - visual action Not in firefox only chrome

  • @klznpch7512
    @klznpch7512 3 года назад

    nice video

  • @RenAok
    @RenAok 4 года назад

    Hello, How should I modify the code to Preview ".pdf" file before file Upload?

  • @sub_o
    @sub_o 4 года назад

    Anyone know how to make it so that the preview image that's larger than the container doesn't overflow it?
    I've tried messing around with the height of it in the css but it always squashes the image rather than sizing it down proportionally.

  • @dhruvinkhant6000
    @dhruvinkhant6000 3 года назад

    can we use for PFD uploder as well ?

  • @CinematicVignettes
    @CinematicVignettes 4 года назад +1

    thank u sir....

  • @shubhamshinde1008
    @shubhamshinde1008 2 года назад

    i can't get .style in my js code help

  • @warpig2786
    @warpig2786 3 года назад

    Does this work on several images? I have an avatar and banner image so far I only found it working with just 1 image but not for 2. Cheers!

    • @mohammadrezabahrami2911
      @mohammadrezabahrami2911 3 года назад

      did you find a solution?

    • @warpig2786
      @warpig2786 3 года назад

      @@mohammadrezabahrami2911 yes I did i might have to create codepen for this

  • @josephdoneecagampang3007
    @josephdoneecagampang3007 4 года назад

    How can I center the image preview in my website?

  • @ShotYoNer
    @ShotYoNer 2 года назад

    from where i get the bash file?!

  • @manohisoaguyaimar7975
    @manohisoaguyaimar7975 4 года назад

    thank you so much

  • @ddc3758
    @ddc3758 4 года назад

    uuuOOOOO!!! you're the best, thanks!!

  • @404movies6
    @404movies6 4 года назад +1

    thanks

  • @melozovitchmoez
    @melozovitchmoez 3 года назад

    good thnx

  • @deviduttasahoo6283
    @deviduttasahoo6283 3 года назад

    I get this message "Uncaught TypeError: Cannot read property 'addEventListener' of null". How to solve it

    • @dcode-software
      @dcode-software  3 года назад

      It means you're trying to select an element that doesn't exist (or doesn't YET exist). As a potential solution, try using the DOMContentLoaded event or put the SCRIPT tag at the end of your BODY tag.

    • @deviduttasahoo6283
      @deviduttasahoo6283 3 года назад

      @@dcode-software okk tq

  • @shubhamsingla4118
    @shubhamsingla4118 4 года назад

    Thanku very much

  • @thoinh177
    @thoinh177 3 года назад

    Is it good to have a high resolution photo?

  • @bessonniy9208
    @bessonniy9208 3 года назад

    thank!

  • @teterochaf
    @teterochaf 3 года назад

    Hi! Thank you for your tutorial (one more subscriber).
    Can I use this code in a contact form 7 (upload file field)?
    How can i keep the proportion of the image with the dimension of the image-preview class so that it doesn't appear distorted?

    • @dcode-software
      @dcode-software  3 года назад

      Thanks for the sub! You can try taking a look at the object-fit CSS property

  • @animania3618
    @animania3618 4 года назад

    Thank you, btw sir how can i save that into mysql using php?

  • @betllatrix534
    @betllatrix534 3 года назад

    Big brain

  • @saqibdiar2522
    @saqibdiar2522 3 года назад

    Sir what about multiple images???

  • @Rere27420
    @Rere27420 2 года назад

    great video, but something went wrong with me I couldn't find exactly where is it

  • @jydee59
    @jydee59 3 года назад

    huge thanks to you bro ....

    • @dcode-software
      @dcode-software  3 года назад +1

      You're welcome

    • @dheerajn3314
      @dheerajn3314 3 года назад

      Why its not working while I have written same code as video in html fiile... Where is missing...
      And tell me about links before head tag... Should I link any other js, css... Files to this file... Help..

  • @lu_sc
    @lu_sc 4 года назад

    Thanks a lot! (:

  • @justsimple6333
    @justsimple6333 4 года назад

    Subscribed