asp.net 文件下载,有进度条
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenameDownload.aspx.cs" Inherits="DownloadProxy.RenameDownload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager runat="server" EnablePageMethods="true"></asp:scriptmanager> <script type="text/javascript"> //PageMethods.set_timeout(5000); PageMethods.set_defaultFailedCallback = "OnFailed"; setInterval(function () { PageMethods.GetDownloadBytes(function (result) { document.getElementById("cellProcess").innerHTML = result; }); }, 1000); function OnFailed(ErrorHander) { var ErrorMessage = 'Timeout:' + ErrorHander.get_timedOut() + ',Error Message:' + ErrorHander.get_message(); ErrorMessage = ErrorMessage + ',Exception Type:' + ErrorHander.get_exceptionType() + ',Error Loaction:' + ErrorHander.get_stackTrace(); alert(ErrorMessage); } function DownloadFile(obj) { PageMethods.DownloadFile("https://download.alipay.com/sec/edit/aliedit.exe", "C:\\Documents and Settings\\kn73\\Desktop\\aliedit.exe"); } </script> <table> <tr> <td><input type="button" value="download" id="btnDownload" onclick="DownloadFile(this)" /></td> <td></td> <td></td> </tr> <tr> <td> </td> <td></td> <td></td> </tr> <tr> <td>Bytes download:</td> <td id="cellProcess"></td> <td></td> </tr> </table> </form> </body> </html>
using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; using System.Threading; namespace DownloadProxy { public partial class RenameDownload : System.Web.UI.Page { public static long DownloadBytes { get; set; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DownloadBytes = 0; } } [System.Web.Services.WebMethod] public static void DownloadFile(String remoteFilename, String localFilename) { var thread = new Thread(new ThreadStart(delegate() { DownloadFileProc(remoteFilename, localFilename); })); thread.Start(); } public static long DownloadFileProc(String remoteFilename, String localFilename) { long bytesProcessed = 0; Stream remoteStream = null; Stream localStream = null; WebResponse response = null; try { // Create a request for the specified remote file name var request = (HttpWebRequest)WebRequest.Create(remoteFilename); request.AllowWriteStreamBuffering = true; request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Proxy = new WebProxy { UseDefaultCredentials = true }; request.MaximumResponseHeadersLength = -1; request.Accept = "www/source, text/html, video/mpeg, image/jpeg, image/x-tiff, image/x-rgb, image/x-xbm, image/gif, application/postscript, application/x-gzip, */*, "; request.AllowAutoRedirect = true; request.UserAgent = "TLE Retriever v1.5.8"; request.ContentType = "application/x-www-form-urlencoded"; request.Method = "GET"; request.Headers.Add("Accept-Language", "zh-cn"); request.Headers.Add("Accept-Encoding", "gzip,deflate"); request.KeepAlive = true; if (request != null) { response = request.GetResponse(); if (response != null) { remoteStream = response.GetResponseStream(); // Create the local file localStream = File.Create(localFilename); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead; // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = remoteStream.Read(buffer, 0, buffer.Length); // Write the data to the local file localStream.Write(buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead; DownloadBytes = bytesProcessed; } while (bytesRead > 0); } } } catch { throw; } finally { if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close(); } // Return total bytes processed to caller. return bytesProcessed; } [System.Web.Services.WebMethod] public static long GetDownloadBytes() { return DownloadBytes; } } }
桂棹兮兰桨,击空明兮溯流光。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!