0

我正在使用基于 blueimp ( http://blueimp.github.com/jQuery-File-Upload ) 和 Ruby on Rails GEM Carrierwave 的多文件上传场景。我遇到的问题是用户可以在文件上传完成之前离开网页。我正在尝试创建一种方法,如果当前正在上传文件并且用户在上传完成之前离开页面,那么它应该返回确认对话框。关于如何做到这一点的任何想法?

4

2 回答 2

1

这可以通过 Javascript 的onbeforeunload事件来完成。

var upload_pending = false;

// ...

// Once upload has started, toggle the boolean flag (in something like
// a click event on the submit button for the form with the file field, 
// making sure to toggle it back after the upload finishes
upload_pending = true;

// ...

window.onbeforeunload = function (e) {
  e = e || window.event;

  if (!upload_pending) return;

  // For IE<8 and Firefox prior to version 4
  if (e) {
    e.returnValue = 'There is a pending upload.';
  }

  // For Chrome, Safari, IE8+ and Opera 12+
  return 'There is a pending upload.';
};
于 2012-07-10T22:48:08.663 回答
1

添加到处理文件上传的表单的末尾。

$(window).bind('beforeunload', function () {
    if ($('#fileupload').data('fileupload')._active) {
        return 'Leaving now would abort your upload. Are you sure?';
    }
});
于 2012-07-10T23:12:22.573 回答