/// <summary> /// 删除文件 /// </summary> /// <param name="path"></param> /// <param name="file"></param> /// <param name="ftpIdent"></param> /// <returns></returns> public static bool DeleteFile(string path, string file, int ftpIdent = 0) { IFTP ftpupload; if (ftpIdent == 0) { ftpupload = CurrentFTP; } else { ftpupload = Instrance(ftpIdent); } //转换路径分割符为"/" //path = path.Replace("\\", "/"); //path = path.StartsWith("/") ? path : "/" + path; FTPConfigInfo ftpConfig = ftpupload.FtpConfig as FTPConfigInfo; //path = ftpConfig.Uploadpath + path; ftpupload.Connect(); string dir = Path.GetDirectoryName(path).Replace("\\", "/"); string[] pathArr = dir.Split('/'); foreach (string pathstr in pathArr) { if (pathstr.Trim() != "") { ftpupload.MakeDir(pathstr); ftpupload.ChangeDir(pathstr); } } ftpupload.RemoveFile(Path.GetFileName(path)); ftpupload.Disconnect();//关闭 return(true); }
/// <summary> /// 设置FTP对象信息 /// </summary> private static void SetFtpConfigInfo() { FTPConfigInfoCollection ftpconfiginfocollection = (FTPConfigInfoCollection)SerializationHelper.Load(typeof(FTPConfigInfoCollection), m_configfilepath); FTPConfigInfoCollection.FTPConfigInfoCollectionEnumerator fcice = ftpconfiginfocollection.GetEnumerator(); //遍历集合并设置相应的FTP信息(静态)对象 while (fcice.MoveNext()) { if (fcice.Current.Name == "ForumAttach") { m_forumattach = fcice.Current; continue; } if (fcice.Current.Name == "SpaceAttach") { m_spaceattach = fcice.Current; continue; } if (fcice.Current.Name == "AlbumAttach") { m_albumattach = fcice.Current; continue; } if (fcice.Current.Name == "MallAttach") { m_mallattach = fcice.Current; continue; } } }
public static IFTP Instrance(int ident) { IFTP ftp = null; FTPConfigInfo ftpConfig = FTPConfigs.GetFTP(ident); if (!string.IsNullOrEmpty(ftpConfig.AssemblyType)) { string[] filenames = ftpConfig.AssemblyType.Split(','); Assembly ass = Assembly.LoadFrom(System.Web.HttpRuntime.BinDirectory + filenames[1] + ".dll"); ftp = (IFTP)Activator.CreateInstance(ass.GetType(filenames[0], false, true), new object[] { (IConfigInfo)ftpConfig }); if (ftp == null) { throw ExceptionManager.MessageException("存储系统配置文件有误,请检查!"); } } else { if (ftpConfig.Timeout == 0) //不限时 { ftp = new FTPClient(ftpConfig.Serveraddress, ftpConfig.Serverport, ftpConfig.Username, ftpConfig.Password, ftpConfig.Mode); } else { ftp = new FTPClient(ftpConfig.Serveraddress, ftpConfig.Serverport, ftpConfig.Username, ftpConfig.Password, ftpConfig.Mode, ftpConfig.Timeout); } } ftp.FtpConfig = ftpConfig; //ftpDic.TryGetValue(ident, out ftp); //if (ftp == null) //{ // FTPConfigInfo ftpConfig = FTPConfigs.GetFTP(ident); // if (!string.IsNullOrEmpty(ftpConfig.AssemblyType)) // { // string[] filenames=ftpConfig.AssemblyType.Split(','); // Assembly ass = Assembly.LoadFrom(System.Web.HttpRuntime.BinDirectory + filenames[1] + ".dll"); // ftp = (IFTP)Activator.CreateInstance(ass.GetType(filenames[0], false, true), new object[] { (IConfigInfo)ftpConfig }); // if (ftp == null) // { // throw ExceptionManager.MessageException("存储系统配置文件有误,请检查!"); // } // } // else // { // if (ftpConfig.Timeout == 0)//不限时 // { // ftp = new FTPClient(ftpConfig.Serveraddress, ftpConfig.Serverport, ftpConfig.Username, ftpConfig.Password, ftpConfig.Mode); // } // else // { // ftp = new FTPClient(ftpConfig.Serveraddress, ftpConfig.Serverport, ftpConfig.Username, ftpConfig.Password, ftpConfig.Mode, ftpConfig.Timeout); // } // } // ftp.FtpConfig = ftpConfig; // ftpDic[ident] = ftp; //} return(ftp); }
/// <summary> /// 判断FTP当前是否配置为本地 /// </summary> /// <param name="ident"></param> /// <returns></returns> public static bool IsLocalhost(int ident) { FTPConfigInfo ftpConfig = null; if (ident > 0) { ftpConfig = FTPConfigs.GetFTP(ident); } else { ftpConfig = FTPConfigs.CurrentFTP; } return(ftpConfig.Serveraddress.ToLower() == "localhost"); }
protected void SaveFtpInfo_Click(object sender, EventArgs e) { string ftpType = GetParam(DNTRequest.GetString("ftptype")); if (Serveraddress.Text.Trim() == "" || Serverport.Text.Trim() == "" || Username.Text.Trim() == "" || Password.Text.Trim() == "" || Uploadpath.Text.Trim() == "" || Timeout.Text.Trim() == "" || Remoteurl.Text.Trim() == "") { base.RegisterStartupScript("", "<script>alert('远程附件设置各项不允许为空');window.location.href='global_ftpsetting.aspx?ftptype=" + ftpType + "';</script>"); return; } if (Uploadpath.Text.EndsWith("/")) { base.RegisterStartupScript("", "<script>alert('附件保存路径不允许以“/”结尾');window.location.href='global_ftpsetting.aspx?ftptype=" + ftpType + "';</script>"); return; } if (Remoteurl.Text.EndsWith("/")) { base.RegisterStartupScript("", "<script>alert('远程访问 URL 不允许以“/”结尾');window.location.href='global_ftpsetting.aspx?ftptype=" + ftpType + "';</script>"); return; } if (!Utils.IsNumeric(Serverport.Text) || int.Parse(Serverport.Text) < 1) { base.RegisterStartupScript("", "<script>alert('远程访问端口必须为数字并且大于1');window.location.href='global_ftpsetting.aspx?ftptype=" + ftpType + "';</script>"); return; } if (!Utils.IsNumeric(Timeout.Text) || int.Parse(Timeout.Text) < 0) { base.RegisterStartupScript("", "<script>alert('超时时间必须为数字并且大于1');window.location.href='global_ftpsetting.aspx?ftptype=" + ftpType + "';</script>"); return; } FTPConfigInfoCollection ftpConfigInfoCollection = (FTPConfigInfoCollection)SerializationHelper.Load(typeof(FTPConfigInfoCollection), Server.MapPath("../../config/ftp.config")); bool isEdit = false; foreach (FTPConfigInfo fci in ftpConfigInfoCollection) { if (fci.Name == ftpType) { fci.Serveraddress = Serveraddress.Text; fci.Serverport = int.Parse(Serverport.Text); fci.Username = Username.Text; fci.Password = Password.Text; fci.Mode = int.Parse(Mode.SelectedValue); fci.Uploadpath = Uploadpath.Text; fci.Timeout = int.Parse(Timeout.Text); fci.Allowupload = int.Parse(Allowupload.SelectedValue); fci.Remoteurl = Remoteurl.Text; fci.Reservelocalattach = int.Parse(Reservelocalattach.SelectedValue); fci.Reserveremoteattach = int.Parse(Reserveremoteattach.SelectedValue); isEdit = true; break; } } if (!isEdit) { FTPConfigInfo fci = new FTPConfigInfo(); fci.Name = ftpType; fci.Serveraddress = Serveraddress.Text; fci.Serverport = int.Parse(Serverport.Text); fci.Username = Username.Text; fci.Password = Password.Text; fci.Mode = int.Parse(Mode.SelectedValue); fci.Uploadpath = ftpType.ToLower(); fci.Timeout = int.Parse(Timeout.Text); fci.Allowupload = int.Parse(Allowupload.SelectedValue); fci.Remoteurl = Remoteurl.Text; fci.Reservelocalattach = int.Parse(Reservelocalattach.SelectedValue); fci.Reserveremoteattach = int.Parse(Reserveremoteattach.SelectedValue); ftpConfigInfoCollection.Add(fci); } SerializationHelper.Save(ftpConfigInfoCollection, Server.MapPath("../../config/ftp.config")); Response.Redirect("global_ftpsetting.aspx?ftptype=" + ftpType); }
public bool UpLoadFile(string path, string file, FTPUploadEnum ftpuploadname) { var fTP = new FTP(); path = path.Replace("\\", "/"); path = (path.StartsWith("/") ? path : ("/" + path)); FTPConfigInfo config = null; bool flag = true; switch (ftpuploadname) { case FTPUploadEnum.ForumAttach: config = GetForumAttachInfo; break; case FTPUploadEnum.SpaceAttach: config = GetSpaceAttachInfo; break; case FTPUploadEnum.AlbumAttach: config = GetAlbumAttachInfo; break; case FTPUploadEnum.MallAttach: config = GetMallAttachInfo; break; case FTPUploadEnum.ForumAvatar: config = GetForumAvatarInfo; break; } fTP = new FTP(config.Serveraddress, config.Serverport, config.Username, config.Password, 1, config.Timeout); path = config.Uploadpath + path; flag = (config.Reservelocalattach != 1); if (!fTP.ChangeDir(path)) { string[] array = path.Split('/'); for (int i = 0; i < array.Length; i++) { string text = array[i]; if (text.Trim() != "") { fTP.MakeDir(text); fTP.ChangeDir(text); } } } fTP.Connect(); if (!fTP.IsConnected) { return(false); } int num = 0; if (!fTP.OpenUpload(file, Path.GetFileName(file))) { fTP.Disconnect(); return(false); } while (fTP.DoUpload() > 0L) { num = (int)(fTP.BytesTotal * 100L / fTP.FileSize); } fTP.Disconnect(); if (flag && Utils.FileExists(file)) { File.Delete(file); } return(num >= 100); }
/// <summary> /// 普通FTP上传文件 /// </summary> /// <param name="file">要FTP上传的文件</param> /// <returns>上传是否成功</returns> public static bool UpLoadFile(string path, string file, UpFileEntity upfileEntity, int ftpIdent = 0) { IFTP ftpupload; FTPConfigInfo ftpConfig = null; //path = ftpConfig.Uploadpath + path; if (upfileEntity.ftpclient == null) { if (ftpIdent == 0) { ftpupload = CurrentFTP; } else { ftpupload = Instrance(ftpIdent); } upfileEntity.ftpclient = ftpupload; //转换路径分割符为"/" path = path.Replace("\\", "/"); path = path.StartsWith("/") ? path : "/" + path; ftpupload.Connect(); //切换到指定路径下,如果目录不存在,将创建 if (!ftpupload.ChangeDir(path)) { string[] pathArr = path.Split('/'); foreach (string pathstr in pathArr) { if (pathstr.Trim() != "") { ftpupload.MakeDir(pathstr); ftpupload.ChangeDir(pathstr); } } } } else { ftpupload = upfileEntity.ftpclient; } if (!ftpupload.IsConnected) { return(false); } ftpConfig = ftpupload.FtpConfig as FTPConfigInfo; int perc = 0; //绑定要上传的文件 if (!ftpupload.OpenUpload(file, System.IO.Path.GetFileName(file))) { //ftpupload.Disconnect(); return(false); } //开始进行上传 while (ftpupload.DoUpload() > 0) { perc = (int)(((ftpupload.BytesTotal) * 100) / ftpupload.FileSize); } //ftpupload.Disconnect(); //删除file参数文件 bool delfile = true; delfile = (ftpConfig.Reservelocalattach == 1) ? false : true; //(如存在)删除指定目录下的文件 if (delfile && FileHelper.FileExists(file)) { System.IO.File.Delete(file); } return((perc >= 100) ? true : false); }