日期:2014-05-17 浏览次数:20430 次
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Threading; using System.IO; using System.Net; using System.Net.Security; public partial class FTPUpload : System.Web.UI.Page { private NetworkCredential certificate; private FtpBLL.FTPGlobalVar FTPVar; protected void Page_Load(object sender, EventArgs e) { FTPVar = FtpBLL.staticFTPGlobalVar.FTPVar; if (FTPVar != null) { UploadFile(FTPVar.Target, FTPVar.user, FTPVar.password, FTPVar.buffer); FtpBLL.staticFTPGlobalVar.FTPVar = null; GC.Collect(); } } private bool UploadFile(string target, string userName, string password, byte[] buffer) { bool bReturn = false; // 根据 ProgressBar.htm 显示进度条界面 string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm"); StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("gb2312")); string html = reader.ReadToEnd(); reader.Close(); Response.Write(html); Response.Flush(); certificate = new NetworkCredential(userName, password); int fsLength = buffer.Length; MemoryStream sourceStreamT = new MemoryStream(buffer); Stream sourceStream = sourceStreamT; string message = GetFileNames(target); int x = 250000; int filemax = 0; if (message != "") { filemax = int.Parse(message.Substring(message.IndexOf(" ") + 1).Substring(0, message.Substring(message.IndexOf(" ") + 1).Length - 2)); } if (filemax == fsLength) { string jsBlock = "<script>ShowMsg('该文件已完成上传。');</script>"; Response.Write(jsBlock); Response.Flush(); return true; } if (filemax != 0) { UploadFile(sourceStream, new Uri(target), filemax, WebRequestMethods.Ftp.UploadFile); } else { UploadFile(sourceStream, new Uri(target), 0, WebRequestMethods.Ftp.UploadFile); } return bReturn; } #region Internal Members #endregion /// <summary> /// 创建FTP请求 /// </summary> private FtpWebRequest CreateFtpWebRequest(Uri uri, string method) { FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri); ftpClientRequest.KeepAlive = false; ftpClientRequest.Proxy = null; ftpClientRequest.Credentials = certificate; ftpClientRequest.KeepAlive = true; ftpClientRequest.UseBinary = true; ftpClientRequest.UsePassive = true; ftpClientRequest.Method = method; //ftpClientRequest.Timeout = 60; return ftpClientRequest; } #region 支持断点续传 public bool UploadFile(Stream sourceStream, Uri destinationPath, int offSet, string ftpMethod) { try { FtpWebRequest request = CreateFtpWebRequest(destinationPath, ftpMethod); request.ContentOffset = offSet; Stream requestStream = null; try { requestStream = request.GetRequestStream();//需要获取文件的流 } catch(Exception ex) { string jsBlock = "<script>alert('" + ex.Message + "');</script>