Wilf
Wilf

Reputation: 2315

How to refresh current page after ajax upload complete

I'm working on JS Ever ajax multiple image uploader. It works really great but one thing. I need the working page reload after all file finish upload. Yes, there's a function to do something after a file uploaded like : success, complete or done. But these callback is per file. It's mean if I add a reload function to it. The page will be reloaded right after the first file is finished upload.

I've tried.

complete:function(file,xhr){alert("ok")}

It works! But it's 3 ok popups since I uploaded 3 files. I need only one "OK" alert after the 3rd file is upload. Please suggest.

$('#uploader').JSAjaxFileUploader({
    uploadUrl: 'upload.php',
    complete: function (file, xhr) {
        $(alert("ok"));
    }
});

This is the DEMO.

Upvotes: 0

Views: 2495

Answers (3)

plondon
plondon

Reputation: 492

Can you just add a count?

var count = 0;
var length;

$('#uploader').JSAjaxFileUploader({
  uploadUrl: 'upload.php',
  complete: function (file, xhr) { 
    if ( count === 0 ) {
      length = $('.JSpreveiw li').length;
    }      
    if ( count === length - 1 ) {
      alert('thanks for uploading!');
      window.location.reload();
    }
    count += 1;
  } 
});

Upvotes: 1

Gaurav Pandey
Gaurav Pandey

Reputation: 2796

Use a hidden field on the page like <input type="hidden" id="hidUploadCount" value="0" /> Now use function beforesend to track current uploads. And use complete function to reload page if the count is upload in progress is 0 i.e.

$('#uploader').JSAjaxFileUploader({
  uploadUrl: 'upload.php',
  beforesend:function(){       
    document.getElementById('hidUploadCount').value+=document.getElementById('hidUploadCount').value;
  },
  complete: function (file, xhr) {
     document.getElementById('hidUploadCount').value-=document.getElementById('hidUploadCount').value;
  if(document.getElementById('hidUploadCount').value)==0)
  {
    alert("ok");//here u can reload the page
  }
  }
});

Upvotes: 0

Pieter De Clercq
Pieter De Clercq

Reputation: 1971

If there is a possibility to get the amount of files uploaded, you can make a function in your "complete" callback which increments a number. When that number equals your amount of files, show the alert and refresh the page.

Upvotes: 0

Related Questions