zoukankan      html  css  js  c++  java
  • 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>&nbsp;</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;
            }
        }
    }
  • 相关阅读:
    HDU 4348 To the moon(可持久化线段树)
    HDU 5875 Function 大连网络赛 线段树
    HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
    HDU 5876 大连网络赛 Sparse Graph
    HDU 5701 中位数计数 百度之星初赛
    CodeForces 708B Recover the String
    Java实现 蓝桥杯 算法提高 套正方形(暴力)
    ASP.NET生成验证码
    ASP.NET生成验证码
    ASP.NET生成验证码
  • 原文地址:https://www.cnblogs.com/nanfei/p/2729610.html
Copyright © 2011-2022 走看看