前阵子碰到这样的问题:在服务器A(192.168.1.1)上点击文件链接,要弹出保存框,提示查看或保存文件,但是文件是保存在服务器B(192.168.1.8)上的,
FileInfo的方法只能操作本地的文件,不支持Uri,遍查资料,确定了WebClient类的方法:
1.第一个发出下载或查看请求的人将触发附件从B服务器到A服务器的下载(使用webClient.DownloadFileAsync方法)
2.此后的请求都是直接通过FileInfo将本地副本以流形式输出
Code1:连接事件
1
//
远程服务器上的文件地址
2 string url = " http://192.168.1.8/upload/123.rar " ;
3 System.Uri remoteUri = new Uri(url);
4 // 本地下载路径地址
5 string localurl = Server.MapPath( " ~/Download/123.rar " );
6 System.Net.WebClient myWebClient = new System.Net.WebClient();
7 FileInfo file = new FileInfo(localurl);
8 try
9 {
10 // 判断本地路径下是否已存在(已有人查看下载过)
11 if ( ! (File.Exists(file.ToString())))
12 {
13 myWebClient.DownloadFileAsync(remoteUri, localurl);
14 myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(myWebClient_DownloadFileCompleted);
15 myWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(myWebClient_DownloadProgressChanged);
16 }
17 else
18 {
19 myWebClient_DownloadFileCompleted( null , null );
20 }
21 }
22 catch (Exception ex)
23 {
24 string strError = ex.Message;
25 }
2 string url = " http://192.168.1.8/upload/123.rar " ;
3 System.Uri remoteUri = new Uri(url);
4 // 本地下载路径地址
5 string localurl = Server.MapPath( " ~/Download/123.rar " );
6 System.Net.WebClient myWebClient = new System.Net.WebClient();
7 FileInfo file = new FileInfo(localurl);
8 try
9 {
10 // 判断本地路径下是否已存在(已有人查看下载过)
11 if ( ! (File.Exists(file.ToString())))
12 {
13 myWebClient.DownloadFileAsync(remoteUri, localurl);
14 myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(myWebClient_DownloadFileCompleted);
15 myWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(myWebClient_DownloadProgressChanged);
16 }
17 else
18 {
19 myWebClient_DownloadFileCompleted( null , null );
20 }
21 }
22 catch (Exception ex)
23 {
24 string strError = ex.Message;
25 }
code2:下载完成后执行DownloadFileCompleted
private
void
myWebClient_DownloadFileCompleted(
object
sender, AsyncCompletedEventArgs e)
{
// 以流形式将本地对远程的副本输出
string localurl = Server.MapPath( " ~/Download/123.rar " );
FileInfo file = new FileInfo(localurl);
Response.Clear();
Response.AddHeader( " Content-Disposition " , " attachment; filename= " + HttpUtility.UrlEncode( " 123.rar " , System.Text.Encoding.UTF8).Replace( " + " , " 20% " ));
Response.AddHeader( " Content-Length " , file.Length.ToString());
Response.ContentType = " application/octet-stream " ;
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
}
{
// 以流形式将本地对远程的副本输出
string localurl = Server.MapPath( " ~/Download/123.rar " );
FileInfo file = new FileInfo(localurl);
Response.Clear();
Response.AddHeader( " Content-Disposition " , " attachment; filename= " + HttpUtility.UrlEncode( " 123.rar " , System.Text.Encoding.UTF8).Replace( " + " , " 20% " ));
Response.AddHeader( " Content-Length " , file.Length.ToString());
Response.ContentType = " application/octet-stream " ;
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
}
code3:下载过程中执行(超过所设上限,下载取消)
1
private
void
DownloadProgressChanged(
object
sender, DownloadProgressChangedEventArgs e)
{
WebClient webClient
=
(WebClient)(sender);
//
Cancel download if we are going to download more than we allow
if (e.TotalBytesToReceive > iMaxNumberOfBytesToAllow)
{
webClient.CancelAsync();
}
}
注意点:
1.由于DownloadFileAsync(Uri, String)方法不会阻止调用线程。需进行如下设置
<%@ Page Language="C#" ...... Async="true" %>
2.DownloadFile 方法将来自 address 参数指定的 URI 的数据下载到本地文件。 此方法在下载资源时(诸如rar类型)阻止。
必须使用DownloadFileAsync方法