question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loading animation while uploading files

See original GitHub issue

I was able to upload mutiple file from surveyjs using XHR upload from the sample. I can also see the total bytes uploaded . Is there any way to show them as the progressbar?

onUploadFiles = (result, options) => {
    const totalCount = options.files.length;
    options.files.map((file, i) => {
      const formData = new FormData();
      
      
      // options.files.forEach(file => {
      formData.append('file', file);
      $.ajax({
        url: UploadURL,
        type: 'POST',
        xhr() {
          const myXhr = $.ajaxSettings.xhr();
          if (myXhr.upload) {
            myXhr.upload.addEventListener(
              'progress',
              event => {
                let percent = 0;
                const position = event.loaded || event.position;
                const total = event.total;
                percent = (position / total) * 100;
                console.log('percent', percent);
                console.log('count', `${i}of${totalCount}`);
              },
              false,
            );
          }
          return myXhr;
        },
        success(data) {
          options.callback(
            'success',
            // multiple file fix
            [file].map(file => {
              return {
                file,
                content: data.secure_url,
              };
            }),
          );
        },
        error(error) {
        },
        async: true,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        timeout: 60000,
      });
    });
  };

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sumitlearntcommented, Nov 7, 2019

@pankajQW @andrewtelnov Thanks for pointing out that bug. Finally I was able to do it for mutiple fileupload as well . Please correct me if anything is wrong

const onUploadFiles = (result, options) => {
        options.files.map((file, i) => {
            const  elements = $("body").find("[aria-label='"+options.name+"']");
            const formData = new FormData();
            formData.append('file', file);
            axios.post(UploadURL,
                formData,
                {
                    onUploadProgress: function (progressEvent) {
                        const percent = parseInt(Math.round((progressEvent.loaded * 100) / progressEvent.total));
                        const html='<div  class="uploadprogress" style="padding: 10px; color:blue;font-weight: bold"> Uploading ' + Math.floor(percent) + '% </div>';
                        elements.parent().find('.uploadprogress').remove();
                        elements.parent().append(html);
                        if(percent===100){
                            elements.parent().find('.uploadprogress').remove();
                        }
                    }.bind(this)
                }
            ).then(function (response) {

                options.callback(
                    'success',
                    // multiple file fix
                    [file].map(file => {
                        return {
                            file,
                            content: response.data.secure_url,
                        };
                    }),
                );
                $('.sv_q_file_remove_button').css('display', 'none');

            })
                .catch(function () {
                    console.log('FAILURE!!');
                    const errorhtml='<div style="padding: 10px; color:red;font-weight: bold">Something went wrong on upload</div>';
                    elements.parent().find('.uploadprogress').remove();
                    elements.parent().append(errorhtml);
                });

        });
    };

1reaction
sumitlearntcommented, Oct 30, 2019

I did something like below , to show progress underneath file uploader

Screen Shot 2019-10-30 at 2 36 01 pm
       const totalCount = options.files.length;
       options.files.map((file, i) => {
           const formData = new FormData();
           formData.append('file', file);
           $.ajax({
               url: url,
               type: 'POST',
               xhr() {
                   const myXhr = $.ajaxSettings.xhr();

                   if (myXhr.upload) {
                       myXhr.upload.addEventListener(
                           'progress',
                           event => {
                               let percent = 0;
                               const position = event.loaded || event.position;
                               const total = event.total;
                               percent = (position / total) * 100;
                               console.log('percent', percent);
                               console.log('count', `${i}of${totalCount}`);
                               $(".sv_q_file div").html('<div style="padding: 10px";font-weight: "bold"> Uploading ' + Math.floor(percent) + '% </div>')
                           },
                           false,
                       );
                   }
                   return myXhr;
               },
               success(data) {
                   $(".sv_q_file div").html('');
                   options.callback(
                       'success',
                       // multiple file fix
                       [file].map(file => {
                           return {
                               file,
                               content: your_file_data,
                           };
                       }),
                   );
                   $('.sv_q_file_remove_button').css('display', 'none');
               },
               error(error) {
                   
                   $(".sv_q_file div").html('<div style="padding: 10px; color:red;font-weight: bold">Something went wrong on upload</div>')

               },
               async: true,
               data: formData,
               cache: false,
               contentType: false,
               processData: false,
               timeout: 60000,
           });
       });
   };

Read more comments on GitHub >

github_iconTop Results From Across the Web

php - How to include loading gif while file upload and insert to ...
To show the loading image when you are uploading, call the showLoading() function jus before the ajax call, and then hide it when...
Read more >
Upload File with Vanilla JavaScript and Loading Animation
In this guide - learn how to upload files in a drop area with HTML, CSS, Vanilla JavaScript, and with a loading animation...
Read more >
CSS Loading Animations: How to Make Them + 15 Examples
Learn how to create a loading animation with CSS that will keep visitors engaged while content loads on your site.
Read more >
Loading animation while uploading files - Jotform
I'm using file uploads and there's really no blatant messaging to tell users that the files are being uploaded. Ideally, (in my mind)...
Read more >
Upload Progress & Loading Animation React Native - YouTube
Now in this video we will see how we can create an upload progress while uploading image to our node js backend from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found