我为用户提供带有ajax更新/进度条的基于表单的文件上传(一次一个)。如果他们离开,我想提醒他们任何当前的上传将被取消。以下代码实现了该目标,但 Chrome 除外 (v.18)。如果您在Chrome中单击后退按钮,文件上传流将立即终止,从而从commons.fileupload库中抛出org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
。
我找不到特定于此问题的任何信息,但是有没有人知道这是否是我现在必须忍受的事情?谢谢。
编辑:我应该补充一点,Chrome仍然显示"离开页面"对话框,但此时上传已经停止,因此答案根本不会影响它。点击 Chrome 中的链接不会停止上传,并且会按预期运行。
window.onbeforeunload =
function(event) {
if (upload_in_progress) {
var msg = "You are uploading a file." +
"If you leave this page the upload " +
"will be cancelled.nnLeave page?";
var event = event || window.event;
if (event) { event.returnValue = msg; }
return msg;
}
};
和Safari中,附加到beforeunload事件的函数必须始终返回字符串值(向用户显示的消息)。