在服务器A中我们可以编写如下代码: <%@ Page Language="C#" EnableSessionState="ReadOnly" Async="true" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Net" %> <!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"> <mce:script type="text/C#" runat="server"><!-- BinaryReader ms; UploadInfo uploadInfo = null; protected void Page_Load(object sender, EventArgs args) { if (this.IsPostBack) { uploadInfo = this.Session["UploadInfo"] as UploadInfo; if (uploadInfo == null) { // 让父页面知道无法处理上传 const string js = "window.parent.onComplete('error', '无法上传文件。请刷新页面,然后再试一次);"; ScriptManager.RegisterStartupScript(this, typeof(upload_aspx), "progress", js, true); } else { // 让服务端知道我们还没有准备好.. uploadInfo.IsReady = false; try { // 读取要上传的文件 FileStream fs = new FileStream(this.fileUpload.PostedFile.FileName, FileMode.Open, FileAccess.Read); ms = new BinaryReader(fs); string newFile = DateTime.Now.Ticks.ToString(); string exit = this.fileUpload.FileName.Substring(this.fileUpload.FileName.IndexOf('.')); newFile = newFile + exit; UriBuilder url = new UriBuilder("http://192.168.25.27:8056/UploadFileHander.ashx");//上传路径 url.Query = string.Format("filename={0}", newFile);//上传url参数 uploadInfo.ContentLength = this.fileUpload.PostedFile.ContentLength; uploadInfo.FileName = newFile; uploadInfo.UploadedLength = 0; //文件存在 初始化... uploadInfo.IsReady = true; WebClient wc = new WebClient(); wc.Credentials = CredentialCache.DefaultCredentials; wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);//委托异步上传事件 wc.OpenWriteAsync(url.Uri);//开始异步上传 const string js = "window.parent.onComplete('success', '{0} 已成功上传,重命名为:{1}:文件大小:{2}');"; ScriptManager.RegisterStartupScript(this, typeof(upload_aspx), "progress", string.Format(js, Path.GetFileName(this.fileUpload.FileName), DateTime.Now.ToString("yyyy-MM") + "/" + DateTime.Now.Day + "/" + newFile, uploadInfo.ContentLength), true); } catch (Exception ex) { Response.Write(ex.Message); } } } } protected void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) { int bufSize = 10; int byteGet = 0; byte[] buf = new byte[bufSize]; while ((byteGet = ms.Read(buf, 0, bufSize)) > 0)//循环读取,上传 { e.Result.Write(buf, 0, byteGet);//注意这里 uploadInfo.UploadedLength += byteGet; } // 让父页面知道已经处理上传完毕 e.Result.Close();//关闭 ms.Close(); } // --></mce:script> <mce:style type="text/css"><!-- BODY{margin:0; padding:0; background-color:#F0F8FF;} --></mce:style><style type="text/css" mce_bogus="1"> BODY{margin:0; padding:0; background-color:#F0F8FF;} </style> </head> <body> <form id="form" runat="server" enctype="multipart/form-data"> <asp:ScriptManager ID="scriptManager" runat="server" /> <mce:script type="text/javascript"><!-- function pageLoad(sender, args){ window.parent.register( $get('<%= this.form.ClientID %>'), $get('<%= this.fileUpload.ClientID %>') ); } // --></mce:script> <div> <asp:FileUpload ID="fileUpload" runat="server" Width="100%" /> </div> </form> </body> </html> 在服务器B中我们创建一个接受文件。UploadFileHander.ashx 来负责文件的接受和保存: using System; using System.Data; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.IO; namespace WebApplication1 { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class UploadFileHander : IHttpHandler { public void ProcessRequest(HttpContext context) { try { string dateNow = DateTime.Now.ToString("yyyy-MM"); // 设置路径 string path = "D://ks5u/NewSystem/" + dateNow + "/" + DateTime.Now.Day; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } string newFile = Path.GetFileName(context.Request.QueryString["filename"]); //文件存在 初始化... long UploadedLength = 0; //缓存 int bufferSize = 1; byte[] buffer = new byte[bufferSize]; // 保存字节 using (FileStream fs = new FileStream(Path.Combine(path, newFile), FileMode.Create)) { while (UploadedLength < context.Request.InputStream.Length) { //从输入流放进缓冲区 int bytes = context.Request.InputStream.Read(buffer, 0, bufferSize); // 字节写入文件流 fs.Write(buffer, 0, bytes); // 更新大小 UploadedLength += bytes; // 线程睡眠 上传就更慢 这样就可以看到进度条了 } } }catch (Exception e) { context.Response.ContentType = "text/plain"; context.Response.Write("上传失败, 错误信息:" + e.Message); } } public bool IsReusable { get { return false; } } } }