/// <summary> /// 判斷指定的文件是否存在 /// </summary> /// <param name="p"></param> /// <returns></returns> public bool IsExists(FtpParameter p) { string name = ComFunc.FindFileName(p.FTP_URL); string dir = p.FTP_URL; if (name.Length > 0) { dir = dir.Replace(name, ""); } StringStd l = ListDirectory(p); StringStd[] ls = l.Split("\r\n"); List <StringStd> lists = new List <StringStd>(); foreach (StringStd s in ls) { lists.Add(s.ToUpper()); } if (lists.Contains(name.ToUpper())) { return(true); } else { return(false); } }
/// <summary> /// 從fpt上找到對應的存在文件的名稱,以便忽略大小寫,提供給download方法使用 /// </summary> /// <param name="p"></param> /// <returns></returns> protected string FindMappingFileName(FtpParameter p) { string name = ComFunc.FindFileName(p.FTP_URL); string dir = p.FTP_URL; if (name.Length > 0) { dir = dir.Replace(name, ""); } string l = ListDirectory(p); string[] ls = l.Split("\r\n"); Dictionary <string, string> lists = new Dictionary <string, string>(); foreach (StringStd s in ls) { lists.Add(s.ToUpper(), s); } return(lists[name.ToUpper()] == null ? "" : lists[name.ToUpper()]); }
/// <summary> /// 下载文件,返回数据流 /// </summary> /// <param name="p"></param> /// <param name="processsing"></param> /// <param name="end"></param> /// <param name="reserveObject"></param> /// <returns></returns> public MemoryStream DownLoadStream(FtpParameter p, Action <FTPStatusData> processsing, Action <FTPStatusData> end, object reserveObject) { try { string name = ComFunc.FindFileName(p.FTP_URL); string dir = p.FTP_URL; if (name.Length > 0) { dir = dir.Replace(name, ""); } //忽略文件名稱大小寫的處理 //if (name.ToUpper() != name) //{ // name = FindMappingFileName(p); //} InitByParameters(p); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse downloadResponse = (FtpWebResponse)ftpRequest.GetResponse(); stream = downloadResponse.GetResponseStream(); string fileName = Path.GetFileName(ftpRequest.RequestUri.AbsolutePath); MemoryStream rtn = null; if (fileName.Length == 0) { rtn = new MemoryStream(); } else { //如果有指定文件名称 rtn = new MemoryStream(); byte[] buffer = new byte[p.BufferSize]; int bytesRead; long transfer = 0; double speed = 0; var st = DateTime.Now; var totalbyte = fileStream.Length; while (true) { bytesRead = stream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } rtn.Write(buffer, 0, bytesRead); transfer += bytesRead; if (processsing != null) { var fsd = new FTPStatusData(); fsd.CurrentByteLength = bytesRead; fsd.TransferedByteLength = transfer; fsd.TotalByteLength = totalbyte; fsd.FileName = Path.GetFileName(name); fsd.CostTime = DateTime.Now - st; var s = fsd.CostTime.TotalMilliseconds; if (s != 0) { speed = (double)transfer / s * 1000; } fsd.Speed = speed; fsd.CurrentStatus = FTPStatusData.FtpStaus.Processing; fsd.ReserveObject = reserveObject; processsing(fsd); } } if (end != null) { var fsd = new FTPStatusData(); fsd.CurrentByteLength = bytesRead; fsd.TransferedByteLength = transfer; fsd.TotalByteLength = totalbyte; fsd.FileName = Path.GetFileName(name); fsd.CostTime = DateTime.Now - st; var s = fsd.CostTime.TotalMilliseconds; if (s != 0) { speed = (double)transfer / s * 1000; } fsd.Speed = speed; fsd.CurrentStatus = FTPStatusData.FtpStaus.End; fsd.ReserveObject = reserveObject; end(fsd); } } return(rtn); } finally { Release(); } }