<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$("#btnDownLoad").on("click", function () {
var $loading = $("#loadingbox");
var downId = new Date().getTime() + "-" + Math.random();
$loading.css("display", "block");
DownLoad($("#downfile").val(), downId);
var tid=setInterval(function () {
$.post("WebForm2.aspx", { getresult: "Y", downid: downId }, function (result) {
//document.writeln("result:" + result);
if(result=="Y")
{
clearInterval(tid);
$loading.css("display", "none");
alert("下载完成!");
}
});
}, 3000);
});
function DownLoad(filename,downid) {
var $form = $("<form target='' method='post' action='WebForm2.aspx'></form>");
$form.append("<input type='hidden' name='filename' value='" + filename + "'>");
$form.append("<input type='hidden' name='downid' value='" + downid + "'>");
$('body').append($form);
$form.submit();
}
});
</script>
</head>
<body>
<input type="button" id="btnDownLoad" value="无刷新下载文件" />
<div id="loadingbox" style="display:none;">
正加下载中,请稍候。。。
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod == "POST")
{
string getresult = Request.Form["getresult"];
string downId=Request.Form["downid"];
string cacheKey =string.Format("downloadcompleted-{0}-{1}",downId,Request.UserHostAddress);
if (getresult == "Y") //判断是否为获取下载结果的请求
{
string result = (Cache[cacheKey] ?? "N").ToString();
Response.Clear();
Response.Write(result);
if(result=="Y") //如果查询到下载完成,则应清除标记下载完成的CACHE
{
Cache.Remove(cacheKey);
}
Response.End();
return;
}
string fileName = Request.Form["filename"];
string localFilePath = Server.MapPath("~/" + fileName);
System.IO.FileInfo file = new System.IO.FileInfo(localFilePath);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Cache.Insert(cacheKey, "Y");//输出所有文件数据后,添加CACHE,并设置downloadcompleted=Y,供页面查询结果使用
Response.End();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有