目前我看到有两个方式:1。用户安装c/s程序2.在服务器端把要下载的文件打成压缩包下下载。
但是他们都有问题就是:第一种要用户下载安装程序。并且这个程序怎么做是个问题,因为我是做asp。net的。
第二种方式就是当文件比较多时,会很占服务器资源的。因为有时会一个人下载很多文件,要是全都弄成压缩包不大现实吧。
大家有没有做过的给点建议吧
但是他们都有问题就是:第一种要用户下载安装程序。并且这个程序怎么做是个问题,因为我是做asp。net的。
第二种方式就是当文件比较多时,会很占服务器资源的。因为有时会一个人下载很多文件,要是全都弄成压缩包不大现实吧。
大家有没有做过的给点建议吧
正则获取URL
再使用webclient等下载文件
MatchCollection mc= Regex.Matches(str, @" <a[^> ]*href=([ ' " "]?)(? <url> [^ ' " "> \s]*)\1?[^> ]*> (? <text> [^ <]*) </a> ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
Response.Write(m.Groups[ "url "].Value);
Response.Write(m.Groups[ "text "].Value);
}
/// 获取网页源代码
/// </summary>
/// <param name="strLink"></param>
/// <returns></returns>
public static string GetHtml(string strLink)
{
Encoding encoding;
StreamReader reader = null;
string strResults;
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
if (strLink.IndexOf(@"http://") >= 0)
{
request = (HttpWebRequest)WebRequest.Create(strLink);
}
else
{
request = (HttpWebRequest)WebRequest.Create(string.Format(@"http://{0}",strLink));
}
switch ((DateTime.Now.Second % 2))
{
case 0:
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;.NET CLR 2.0.50727)";
break; default:
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
break;
}
response = (HttpWebResponse)request.GetResponse(); if (response.CharacterSet != null && response.CharacterSet.ToLower() == "utf-8")
{
encoding = Encoding.UTF8;
}
else
{
encoding = Encoding.GetEncoding(936);
}
reader = new StreamReader(response.GetResponseStream(), encoding);
strResults = Regex.Replace(reader.ReadToEnd(), @"[\r\n]", " ", RegexOptions.IgnoreCase);
if (strLink != response.ResponseUri.ToString())
{
strResults = "|RedirectWeb|" + strResults;
}
return strResults;
}
catch
{
return string.Empty;
}
finally
{
if (request != null)
{
request.Abort();
}
if (response != null)
{
response.Close();
}
if (reader != null)
{
reader.Close();
reader.Dispose();
}
} }
然后用正则表达式把里面的所有连接取出来.
然后循环去取出连接内的文件.很多网页连接都是相对连接,利用
C# code public Uri fixurl(string url, Uri baseurl)
{
if (url.IndexOf("javascript:") >= 0) { return null; }
Uri u = null;
Uri.TryCreate(baseurl, url, out u);
return u;
}来获取绝对连接.然后利用
HttpWebRequest 和HttpWebResponse 下载文件.
下载和获取网页都不烦,最烦的还是网页编码.很多网页的编码那个乱哦.
/// <summary>
/// 压缩方法
/// </summary>
/// <param name="patch">预压缩的路径</param>
/// <param name="rarPatch">压缩后的路径</param>
/// <param name="rarName">压缩文件名</param>
public static void RARsave(string patch, string rarPatch, string rarName)
{
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRAR.exe\\Shell\\Open\\Command");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
Directory.CreateDirectory(patch);
//命令参数 //the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩 the_Info = " a " + rarName + " " + patch + " -r"; ;
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录 the_StartInfo.WorkingDirectory = rarPatch;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
///解 压缩方法
/// </summary>
/// <param name="patch">预解压缩的路径</param>
/// <param name="rarPatch">解压缩后的路径</param> public static int unRAR(string unRarPatch, string rarPatch)
{ try
{
System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = true;
Process1.StartInfo.Arguments = " e " + unRarPatch + " " + rarPatch + " -y";
Process1.Start();
Process1.WaitForExit();
if (Process1.HasExited)
{
return Process1.ExitCode;
}
return -1000;
}
catch (Exception ex)
{
return -1001;
}
}
参考
这个当文件很多时比如说40多个7M大的pdf文件。。压缩的过程会很耗费资源吧