dongxingdu9594
dongxingdu9594
2014-12-28 15:07
浏览 349

如何在ajax上传完成后刷新当前页面

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubeishuai6598
    doubeishuai6598 2014-12-28 15:15
    已采纳

    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;
      } 
    });
    
    点赞 评论
  • dougu5950
    dougu5950 2014-12-28 15:12

    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.

    点赞 评论
  • dsgwoh7038
    dsgwoh7038 2014-12-28 15:19

    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
      }
      }
    });
    
    点赞 评论

相关推荐