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;
            }
        }
    }
  • 相关阅读:
    Android Market google play store帐号注册方法流程 及发布应用注意事项【转载】
    cocos2d-x 调用第三方so文件
    ios cocos2d-x 多点触摸
    linux文件分割(将大的日志文件分割成小的)
    Linux 统计某个字符串出现的次数
    scapy基础-网络数据包结构
    mac 下idea光标问题
    mac ox终端显示 bogon的问题
    hibernate和mybatis的区别
    memcached知识点梳理
  • 原文地址:https://www.cnblogs.com/nanfei/p/2729610.html
Copyright © 2011-2022 走看看