<%@ 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 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" onclick="DownloadFile(this)" /></td>
<td></td>
<td></td>
</tr>
<tr>
<td> </td>
<td></td>
<td></td>
</tr>
<tr>
<td>Bytes download:</td>
<td ></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;
}
}
}