Creating an AJAX File Upload Progress Bar in JavaScript - Tutorial For Beginners

Поділитися
Вставка
  • Опубліковано 10 гру 2024

КОМЕНТАРІ • 89

  • @dcode-software
    @dcode-software  5 років тому +3

    Check out my video on creating your own custom file upload button:
    ua-cam.com/video/T3PDgtliezo/v-deo.html

    • @Crimin4L
      @Crimin4L 3 роки тому +1

      You never even used the const inpfile so the file never gets passed through and actually uploaded. what would that line of code be? unless you passed it through the xhr.send(new FormData(uploadForm)) in that case it didnt work either. EDIT: Figured out that its this line of code:
      xhr.setRequestHeader("Content-Type", "multipart/form-data");
      Removed it and works like a charm

  • @NarendraKumar-tc3bm
    @NarendraKumar-tc3bm 5 років тому +10

    I was looking for the actual progress bar tutorial for a very long time..finally got it...thanks!!

  • @LeonardoSantos-mb6dq
    @LeonardoSantos-mb6dq 4 роки тому +3

    The simplest and cleanest way! Thank you very much!

  • @js-javascript1706
    @js-javascript1706 3 роки тому +1

    Your content never disappoints! You are really good at explaining things and you do so in a really short amount of time. You deserve way more subscribers than you have now. You are definitely one of the most underrated web developer channels on youtube. I love your content. I hope you never stop making such amazing content!

  • @m4cdemos
    @m4cdemos 3 роки тому

    excellent - very clear and I like the way that you touch upon the 'why' not just the 'how'. Thanks.

  • @arturorojas1062
    @arturorojas1062 5 років тому +4

    A heads up for anyone who wants to use Laravel as the backend for this example (like I did). Do not use xhr.setContentHeader(), otherwise Laravel won't be able to get the file out of the request, instead, set the enctype in the form tag directly.

    • @charlesmendez4686
      @charlesmendez4686 5 років тому

      likewise there is either a problem with moz or shared hosting that messes up with the headers. I am using php and ajax and it was not working till i deleted that xhr.set...(). I did not add the headers to my form as well because then the form submit will want a method and form action that will result in the reload crap that occurs when you submit a form normally. I think the new browser updates have taken care of headers issue on when you call formdata()

  • @club-blueentertainments9298
    @club-blueentertainments9298 4 роки тому +3

    the best i have come across so far great work done

  • @Crimin4L
    @Crimin4L 3 роки тому +7

    If the Progress bar works but the file isn't uploading, its because you have to *remove* this line of code:
    *_xhr.setRequestHeader("Content-Type", "multipart/form-data");_*
    Please thumbs up so others that have the same problem can see the solution.

    • @MasudRana-mn2xl
      @MasudRana-mn2xl 3 роки тому

      You can use this to solve your problem.

    • @Crimin4L
      @Crimin4L 3 роки тому

      @@MasudRana-mn2xl yes but in this tutorial he says to add the line *_xhr.setRequestHeader("Content-Type", "multipart/form-data")_* in js which breaks the operation; Just removing it will do, but if you still need to set the content type then yes, you can do what you suggested.

  • @lanasvetas
    @lanasvetas Рік тому

    thank you so much, very clear and easy to understand!!

  • @paulbarker5295
    @paulbarker5295 2 роки тому +1

    Could I use this, or a similar script, if I only wanted to monitor the progress of an action on the remote server, like copying files from one location to another, is it even possible?

  • @nocacad
    @nocacad 5 років тому +2

    Can you make upload file multiple with progress bar and abort function?

  • @YosipmikeColin
    @YosipmikeColin 4 роки тому +1

    mi barra se crece mas de lo norma y al seleccionar otro archivo no carga de nuevo, se queda en 100 que hago ??

  • @Hannsfeld
    @Hannsfeld 3 роки тому +1

    So if I understand your code correctly, you are monitoring the document for the file upload object to create your progress bar completion percentage. I can see the JavaScript having easy access to this object in the source page, but what if you have a PHP script on the backend which is an AJAX target and you want that script to return progress for thing's it's doing? I have wracked my brain for years trying to find a way to do this, but the closest thing I could muster is to use the AJAX callback when it completes to know to do an action on the calling page.
    I have a current script that has to send over 800 emails to our organization, and the only thing I can return for this AJAX invoked emailer is it's completion. Since the emailing takes quite a while, it always looks like the browser has crashed waiting on execution. I just have to create messages to 'please wait' and not to navigate away until the script completes. It would be so nice to have a means of returning progress for arbitrary backend script actions. Do you have any ideas on this?

  • @Rafael-es5dp
    @Rafael-es5dp 8 місяців тому

    Good day. What if the input file is multiple. Is the computation the same?

  • @lergolas2
    @lergolas2 4 роки тому +1

    I need show messages from upload.php, but with the progress bar script, this messages are hidden. Can you help me with this problem. I´m a noob with Ajax. Thanks!

  • @Harshitsharma-pu9fe
    @Harshitsharma-pu9fe 2 роки тому

    405 Method not allowed. This error is coming at the point where I mentioned the POST method. what could be the solution ?

  • @dijiflex
    @dijiflex 5 років тому +1

    Can you please make the same video using fetch api?

  • @junit85
    @junit85 4 роки тому +1

    You are the man, thanks alot.

  • @kimitori379
    @kimitori379 Рік тому +1

    thx a lot =)

  • @ai.201
    @ai.201 3 роки тому

    Is there any way we can make this when the data come back from sever. Because it is working fine with submission of form but I want to create it when the data comes back from sever.

  • @peberdah
    @peberdah Рік тому

    Why don't you use the tag to simplify the example. Example is understandable but it does not work for me, specially the enctype parameter. Source code link please.

  • @paaabdlpptb270
    @paaabdlpptb270 4 роки тому

    Hi, Thanks a lot for your job.
    So I have a question. How do I do if I don't want use a select file ?
    I know the path file and it change everytime. I don't want users does a selection.
    Thanks.

  • @maud.nguyen1474
    @maud.nguyen1474 4 роки тому

    This helps me a lot. Thank you!

  • @AlirezaSalehi-xz3ch
    @AlirezaSalehi-xz3ch 3 роки тому

    that was amazing thanks a lot

  • @AndyChamberlainMusic
    @AndyChamberlainMusic 4 роки тому

    what was `const inpFile` for, you didn't use it in the js

  • @lllxxBrunoxxlll
    @lllxxBrunoxxlll 3 роки тому

    Thank you .. Work for me

  • @Raaaww
    @Raaaww 4 роки тому

    Thank you, I learned a lot.

  • @gilbesm1
    @gilbesm1 3 роки тому

    What library are you referencing for class="form"

    • @fredphillips547
      @fredphillips547 2 роки тому

      he says it is merely aesthetic so it can be edited with css

  • @bigbillionstartup4122
    @bigbillionstartup4122 3 роки тому

    Thanks a lot

  • @rbelatamas
    @rbelatamas 3 роки тому

    Thank you so much!

  • @ShinAkuma
    @ShinAkuma 4 роки тому

    Neat, is there a way to implement this using jQuery ajax ?

  • @rabbithome9698
    @rabbithome9698 4 роки тому +1

    Always i get good experience

  • @RaajKanchan
    @RaajKanchan 5 років тому +2

    Thank you so much

  • @i.am.rossalex
    @i.am.rossalex 3 роки тому

    How to do the same with fetch, not with xhr?

  • @_zxcsv
    @_zxcsv 4 роки тому

    Is this compatible with Fetch API?

  • @RenderHub
    @RenderHub 4 роки тому

    This is not working for me. The file appears to be uploading via the chrome console, but the $_FILES variable is always NULL, so I cant grab the file on the receiving end.
    Any help is appreciated :)
    Here is my code...
    const uploadForm = document.getElementById("uploadForm");
    const inpFile = document.getElementById("inpFile");

    uploadForm.addEventListener("submit", uploadFile);

    function uploadFile(e) {
    e.preventDefault();
    const xhr = new XMLHttpRequest();
    xhr.open("POST", "ul.php");
    xhr.upload.addEventListener("progress", e => {
    console.log(e);
    });

    xhr.setRequestHeader("Content-type", "multipart/form-data");
    xhr.send(new FormData(uploadForm));
    }

  • @thenahidul
    @thenahidul 5 років тому

    Nice. Can you please show the same thing using jQuery Ajax?

  • @xEvilGod
    @xEvilGod 4 роки тому

    dear, i have the "upload.php" done inside the html and not as seperate file, now i cant do ajax.open("post", "upload.php")
    is there an alternative way to do this?

    • @xEvilGod
      @xEvilGod 4 роки тому

      @Ketan Artist yes I have. Homemade solved

    • @xEvilGod
      @xEvilGod 4 роки тому

      @Ketan Artist but doesn't show any php echo properties. But runs the codes as it should.

  • @wanderingsoul2909
    @wanderingsoul2909 2 роки тому

    Hi, can anyone comment on using this method for large file uploads 25-50MB. I assume this is a synchronous process . Would async javascript file uploader be a better choice ? Thanks :)

  • @chriscardoz
    @chriscardoz 5 років тому +1

    Love it

  • @takikauser
    @takikauser 3 роки тому

    need this one using asp.net mvc

  • @curtiscrentsil2836
    @curtiscrentsil2836 4 роки тому

    Dom, please could you do this same tutorial with fetch API, thank you

    • @dcode-software
      @dcode-software  4 роки тому

      Hey Curtis, yes that's definitely written down in my list of topics I want to cover - unfortunately Fetch doesn't make this as easy as AJAX :(

    • @curtiscrentsil2836
      @curtiscrentsil2836 4 роки тому

      @@dcode-software maybe I should just switch my codes to ajax :( ,...thanks

  • @wu1686
    @wu1686 5 років тому

    Hi I can not find my upload picture in my uploads folder, can you help me?

    • @dcode-software
      @dcode-software  5 років тому

      Try checking the $_FILES array first to see if PHP has at least received your file, and then work from there

    • @hunnidbands100
      @hunnidbands100 3 роки тому

      @FREDDY JOHAN LOOR ALARCON can you explain please? i just sit for 30 minutes trying to solve the problem and i was ignoring your comment searching another advices, but exactly your advice helped me. xmlhttpcontent adds the header automatically?

  • @mosesfahmipratama5378
    @mosesfahmipratama5378 3 роки тому

    Wow i'ts cool tutorial..

  • @farhanyousaf5616
    @farhanyousaf5616 4 роки тому

    Can this be done with fetch?

    • @dcode-software
      @dcode-software  4 роки тому +1

      Yes but I believe it's very early standard, it might be safer to use something like Axios if you really need this functionality

    • @farhanyousaf5616
      @farhanyousaf5616 4 роки тому

      @@dcode-software Wow, thank you for responding so quickly! I'd love to pick up any courses you may have to offer.
      I had more one question, what if I want to display a progress of a server task (written in Go with Vue front end -- which would spit JSON with a percentage that I can display on the page, but not sure how to poll it (long polling, vs. SSE). Thoughts?

    • @dcode-software
      @dcode-software  4 роки тому

      Hmm, I don't see why polling wouldn't work, but I think using something like WebSockets is ideal

    • @farhanyousaf5616
      @farhanyousaf5616 4 роки тому

      @@dcode-software I think polling might be the easiest solution at 500 ms or so. WS would be a bit more work. Have you played with SSE? One way WS, kind of.

    • @dcode-software
      @dcode-software  4 роки тому

      I haven't no 🙁

  • @wentanzhao8640
    @wentanzhao8640 4 роки тому

    Hi dcode. where is the code for this video?

  • @rashedulhasan5372
    @rashedulhasan5372 4 роки тому +2

    cool tutorials but codes aren't available. Can we please get the codes with each video

    • @Smartstv
      @Smartstv 4 роки тому +1

      phppot.com/downloads/jquery-ajax-image-upload-with-animating-progress-bar.zip

  • @amirrezam75
    @amirrezam75 4 роки тому

    Nice

  • @anouartouil3645
    @anouartouil3645 4 роки тому

    first , thank you so much for the video
    i get a typeerror
    the progressBarFill is null

  • @natqe4049
    @natqe4049 5 років тому +3

    wow

  • @eliuespinoza4016
    @eliuespinoza4016 5 років тому

    Its working but it doesnt upload the files. Then?

    • @dcode-software
      @dcode-software  5 років тому

      You'll need to process the file upload on the server side

    • @eliuespinoza4016
      @eliuespinoza4016 5 років тому

      ​@@dcode-software Could you please tell What di you mean?

    • @eliuespinoza4016
      @eliuespinoza4016 5 років тому

      @@dcode-software My uploading is working perfectly. Once i use your script. the progress bar works well. but it does not upload anything

    • @dcode-software
      @dcode-software  5 років тому

      Are you using PHP?

    • @eliuespinoza4016
      @eliuespinoza4016 5 років тому

      @@dcode-software YEs

  • @Taher_M
    @Taher_M Рік тому

    I couldn't access file in php code using $_FILES['file']['name']
    because of an error of "Missing boundary in multipart/form-data POST data in Unknown on line 0"
    then after searching I found out you also need to set the boundary in setRequestHeader() method,
    for example :
    hr.setRequestHeader("Content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));
    otherwise all you need to do is remove the following line:
    xhr.setRequestHeader("Content-Type", "multipart/form-data");
    I spent a lot of time finding the solution to the error so I thought of sharing my experience to help others in the future

  • @factteczmalayalam3114
    @factteczmalayalam3114 4 роки тому

    Why you only have 29k subs..,?

  • @Kodeispoetry
    @Kodeispoetry 4 роки тому

    Source Code sir?

  • @a-technology7177
    @a-technology7177 4 роки тому

    Cool

  • @mquanit3333
    @mquanit3333 5 років тому

    best

  • @sapnanegi3447
    @sapnanegi3447 3 роки тому

    Thank you so much