完成ASP.NET無刷新下載並提醒下載完成的開辟思緒。本站提示廣大學習愛好者:(完成ASP.NET無刷新下載並提醒下載完成的開辟思緒)文章只能為提供參考,不一定能成為您想要的結果。以下是完成ASP.NET無刷新下載並提醒下載完成的開辟思緒正文
先給年夜家貼代碼了,前面給年夜家在做詳細的文字解釋。
以下是前端代碼:
<%@ 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="text" id="downfile" />
<input type="button" id="btnDownLoad" value="無刷新下載文件" />
<div id="loadingbox" >
正加下載中,請稍候。。。
</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();
}
}
}
}
完成道理:前端經由過程靜態創立FORM用來要求下載的資本,要求參數中必須包括一個downId(我這裡還包括了一個要下載的文件名),這個是與辦事器真個cache Key絕對應的,辦事器端吸收到下載要求後,會獲得下載的文件名及downId,然後根據downId生成一個絕對應的cache Key用於標識下載成果,再根據下載文件名獲得辦事器的文件資本並呼應輸入文件流供客戶端下載,輸入終了後,生成一個Cache,並標志為Y,注解已輸入終了。客戶端鄙人載文件的同時,會每隔3秒要求辦事器獲得下載完成的Cache標識,若獲得到其值為Y,則注解下載完成,辦事器立刻消除該Cache,客戶端作出響應的呼應(好比:封閉提醒下載的對話框及彈出下載完成的對話框)
後果以下:

經由多個分歧的閱讀器及年夜文件壓力測試,兼容性優越,都能正常下載並能收到下載完成提醒,基於以上道理,可以完成進度條顯示,完成道理簡述:客戶端要求辦事器下載資本-->辦事器呼應並按字節分段順次輸入,每次輸入時生成CACHE,並保留輸入進度,直至全體輸入終了,客戶端在要求辦事器下載資本的同時,也須要同時每隔幾秒要求查詢辦事器下載進度,直至下載進度為100%停滯要求。也可應用HTML5新特征WEBSOCKET技巧來完成。