欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

實現(xiàn)ASP.NET無刷新下載并提示下載完成的開發(fā)思路

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

先給大家貼代碼了,后面給大家在做具體的文字說明。

以下是前端代碼:

<%@ 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" style="display:none;">
    正加下載中,請稍候。。。
  </div>
</body>
</html>

以下是服務(wù)器端代碼:

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") //判斷是否為獲取下載結(jié)果的請求
        {
          string result = (Cache[cacheKey] ?? "N").ToString();
          Response.Clear();
          Response.Write(result);
          if(result=="Y") //如果查詢到下載完成,則應(yīng)清除標記下載完成的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");//輸出所有文件數(shù)據(jù)后,添加CACHE,并設(shè)置downloadcompleted=Y,供頁面查詢結(jié)果使用
        Response.End();
      }
    }
  }
}

實現(xiàn)原理:前端通過動態(tài)創(chuàng)建FORM用來請求下載的資源,請求參數(shù)中必需包含一個downId(我這里還包含了一個要下載的文件名),這個是與服務(wù)器端的cache Key相對應(yīng)的,服務(wù)器端接收到下載請求后,會獲取下載的文件名及downId,然后依據(jù)downId生成一個相對應(yīng)的cache Key用于標識下載結(jié)果,再依據(jù)下載文件名獲取服務(wù)器的文件資源并響應(yīng)輸出文件流供客戶端下載,輸出完畢后,生成一個Cache,并標記為Y,表明已輸出完畢??蛻舳嗽谙螺d文件的同時,會每隔3秒請求服務(wù)器獲取下載完成的Cache標識,若獲取到其值為Y,則表明下載完成,服務(wù)器立即清除該Cache,客戶端作出相應(yīng)的響應(yīng)(比如:關(guān)閉提示下載的對話框及彈出下載完成的對話框)

效果如下:

 

經(jīng)過多個不同的瀏覽器及大文件壓力測試,兼容性良好,都能正常下載并能收到下載完成提示,基于以上原理,可以實現(xiàn)進度條顯示,實現(xiàn)原理簡述:客戶端請求服務(wù)器下載資源-->服務(wù)器響應(yīng)并按字節(jié)分段依次輸出,每次輸出時生成CACHE,并保存輸出進度,直至全部輸出完畢,客戶端在請求服務(wù)器下載資源的同時,也需要同時每隔幾秒請求查詢服務(wù)器下載進度,直至下載進度為100%停止請求。也可利用HTML5新特性WEBSOCKET技術(shù)來實現(xiàn)。

上一篇:C#接口實現(xiàn)方法實例分析

欄    目:C#教程

下一篇:C#繪制曲線圖的方法

本文標題:實現(xiàn)ASP.NET無刷新下載并提示下載完成的開發(fā)思路

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6922.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有