using System;
using System.IO;
using System.Net;
/// <summary>
/// DownloadFile 的摘要说明
/// </summary>
public class DownloadFile
{
private static string FileName, FileUrl;
private static string[] UrlArray = null;
private static Stream stream = null;
private static HttpWebResponse httpResponse = null;
private static HttpWebRequest HWRequest;
private static double Length;
private static byte[] buffer = new byte[4096];
public DownloadFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string StartDownloadFile(string Url)
{
FileUrl = Url;
UrlArray = FileUrl.Split(new string[] { "/" }, StringSplitOptions.None);
FileName = System.Web.HttpContext.Current.Server.MapPath(UrlArray[UrlArray.Length - 1].ToString() + ".temp");
HWRequest = (HttpWebRequest)WebRequest.Create(FileUrl);
try
{
httpResponse = (HttpWebResponse)HWRequest.GetResponse();
}
catch (WebException e)
{
HWRequest.Abort();
return FileUrl + " 下载失败" + e.Message;
}
//stream = httpResponse.GetResponseStream();
Length = Convert.ToDouble(httpResponse.ContentLength) / 1024;
try
{
StartDownload();
}
catch (WebException err)
{
HWRequest.Abort();
httpResponse.Close();
return FileUrl + " 文件大小:" + string.Format("{0:F}", Length) + "KB " + "下载失败";
}
return FileUrl + " 文件大小:" + string.Format("{0:F}", Length) + "KB " + "下载成功";
}
public static string StartDownloadFile(string Url, string SavaPath)
{
FileUrl = Url;
UrlArray = FileUrl.Split(new string[] { "/" }, StringSplitOptions.None);
FileName = System.Web.HttpContext.Current.Server.MapPath(UrlArray[UrlArray.Length - 1].ToString() + ".temp");
HWRequest = (HttpWebRequest)WebRequest.Create(FileUrl);
try
{
httpResponse = (HttpWebResponse)HWRequest.GetResponse();
}
catch (WebException e)
{
HWRequest.Abort();
return FileUrl + " 下载失败" + e.Message;
}
//stream = httpResponse.GetResponseStream();
Length = Convert.ToDouble(httpResponse.ContentLength) / 1024;
try
{
StartDownload();
}
catch (WebException err)
{
HWRequest.Abort();
httpResponse.Close();
return FileUrl + " 文件大小:" + string.Format("{0:F}", Length) + "KB " + "下载失败";
}
return FileUrl + " 文件大小:" + string.Format("{0:F}", Length) + "KB " + "下载成功";
}
private static void StartDownload()
{
if (File.Exists(FileName)) File.Delete(FileName);
if (File.Exists(FileName.Replace(".temp", ""))) File.Delete(FileName.Replace(".temp", ""));
- 分类:asp.net|阅读 (2010)|收 藏|分 享|打 印