思路:1 当submit后 1.1 后台接受请求,开始处理,处理完成后,准备下载文件,下载时给客户端写入cookie信息
1.2 当submit后,后台在处理,前台在调用setInterview,间隔的获取后台相应写入到站点的cookie值,如果该值被写
入后,在前台是客运获取到的,说明下载成功了 2 下载成功后清楚cookie和计时器
前段代码
1 var VehicleNoPar = $("<input>"); 2 VehicleNoPar.attr("type", "hidden"); 3 VehicleNoPar.attr("name", "DownloadBookingIDs"); 4 VehicleNoPar.attr("value", DownloadBookingIdVal); 5 form.append(VehicleNoPar); 6 7 //将表单放置在web中 8 $("body").append(form); 9 10 var interval = setInterval( 11 function () { 12 var downloadSucess = getCookie("transReplayDownload"); 13 if (downloadSucess != undefined && downloadSucess != null && downloadSucess == "transReplayDownloadVal") { 14 15 clearInterval(interval); 16 deleteCookie("transReplayDownload"); 17 18 // 下载成功,刷新列表数据 19 SearchData(); 20 } 21 else { 22 alert("下载中"); 23 } 24 }, 25 1000, 26 null 27 ); 28 29 //表单提交 30 form.submit();
后台代码
1 string userAgent = Request.ServerVariables["http_user_agent"].ToLower(); 2 if (userAgent.IndexOf("firefox", System.StringComparison.Ordinal) == -1) 3 { 4 fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(fileName)); 5 } 6 Response.AppendCookie(new HttpCookie("transReplayDownload", "transReplayDownloadVal")); 7 Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); 8 Response.Charset = "GB2312"; 9 Response.ContentEncoding = System.Text.Encoding.UTF8; 10 Response.ContentType = "application/ms-excel"; 11 Response.WriteFile(filePath); 12 Response.End();