示例#1
0
        /// <summary>
        /// 上传远程附件
        /// </summary>
        /// <param name="path">要上传的文件路径(网络地址)</param>
        /// <param name="file">要上传的文件(本地地址)</param>
        /// <param name="ftpuploadname">远程FTP类型(论坛,空间或相册)</param>
        /// <returns>是否成功</returns>
        public bool UpLoadFile(string path, string file, FTPUploadEnum ftpuploadname)
        {
            FTP ftpupload = new FTP();
            //转换路径分割符为"/"
            path = path.Replace("\\", "/");
            path = path.StartsWith("/") ? path : "/" +path ;

            //删除file参数文件
            bool delfile = true;
            //根据上传名称确定上传的FTP服务器
            switch (ftpuploadname)
            {
                //论坛附件
                case FTPUploadEnum.ForumAttach:
                    {
                        ftpupload = new FTP(GetForumAttachInfo.Serveraddress, GetForumAttachInfo.Serverport, GetForumAttachInfo.Username, GetForumAttachInfo.Password, 1, GetForumAttachInfo.Timeout);
                        path = GetForumAttachInfo.Uploadpath + path;
                        delfile = (GetForumAttachInfo.Reservelocalattach == 1) ? false : true;
                        break;
                    }

                //空间附件
                case FTPUploadEnum.SpaceAttach:
                    {
                        ftpupload = new FTP(GetSpaceAttachInfo.Serveraddress, GetSpaceAttachInfo.Serverport, GetSpaceAttachInfo.Username, GetSpaceAttachInfo.Password, 1, GetSpaceAttachInfo.Timeout);
                        path = GetSpaceAttachInfo.Uploadpath + path;
                        delfile = (GetSpaceAttachInfo.Reservelocalattach == 1) ? false : true;
                        break;
                    }

                //相册附件
                case FTPUploadEnum.AlbumAttach:
                    {
                        ftpupload = new FTP(GetAlbumAttachInfo.Serveraddress, GetAlbumAttachInfo.Serverport, GetAlbumAttachInfo.Username, GetAlbumAttachInfo.Password, 1, GetAlbumAttachInfo.Timeout);
                        path = GetAlbumAttachInfo.Uploadpath + path;
                        delfile = (GetAlbumAttachInfo.Reservelocalattach == 1) ? false : true;
                        break;
                    }
                //商城附件
                case FTPUploadEnum.MallAttach:
                    {
                        ftpupload = new FTP(GetMallAttachInfo.Serveraddress, GetMallAttachInfo.Serverport, GetMallAttachInfo.Username, GetMallAttachInfo.Password, 1, GetMallAttachInfo.Timeout);
                        path = GetMallAttachInfo.Uploadpath + path;
                        delfile = (GetMallAttachInfo.Reservelocalattach == 1) ? false : true;
                        break;
                    }
                //论坛头像
                case FTPUploadEnum.ForumAvatar:
                    {
                        ftpupload = new FTP(GetForumAvatarInfo.Serveraddress, GetForumAvatarInfo.Serverport, GetForumAvatarInfo.Username, GetForumAvatarInfo.Password, 1, GetForumAvatarInfo.Timeout);
                        path = GetForumAvatarInfo.Uploadpath + path;
                        delfile = (GetForumAvatarInfo.Reservelocalattach == 1) ? false : true;
                        break;
                    }
            }

            //切换到指定路径下,如果目录不存在,将创建
            if (!ftpupload.ChangeDir(path))
            {
                foreach (string pathstr in path.Split('/'))
                {
                    if (pathstr.Trim() != "")
                    {
                        ftpupload.MakeDir(pathstr);
                        ftpupload.ChangeDir(pathstr);
                    }
                }                
            }
            
            ftpupload.Connect();

            if (!ftpupload.IsConnected)
                return false;

            int perc = 0;

            //绑定要上传的文件
            if (!ftpupload.OpenUpload(file, System.IO.Path.GetFileName(file)))
            {
                ftpupload.Disconnect();
                return false;
            }
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
           
            //开始进行上传
            while (ftpupload.DoUpload() > 0)
                perc = (int)(((ftpupload.BytesTotal) * 100) / ftpupload.FileSize);

            ftpupload.Disconnect();

            //long elapse = sw.ElapsedMilliseconds;
            //sw.Stop();    

            //(如存在)删除指定目录下的文件
            if (delfile && Utils.FileExists(file))
                System.IO.File.Delete(file);          

            return (perc >= 100) ? true : false;
        }
示例#2
0
        /// <summary>
        /// FTP连接测试
        /// </summary>
        /// <param name="Serveraddress">FTP服务器地址</param>
        /// <param name="Serverport">FTP端口</param>
        /// <param name="Username">用户名</param>
        /// <param name="Password">密码</param>
        /// <param name="Timeout">超时时间(秒)</param>
        /// <param name="uploadpath">附件保存路径</param>
        /// <param name="message">返回信息</param>
        /// <returns>是否可用</returns>
        public bool TestConnect(string Serveraddress, int Serverport, string Username, string Password, int Timeout, string uploadpath, ref string message)
        {
            FTP ftpupload = new FTP(Serveraddress, Serverport, Username, Password, 1,  Timeout);
            bool isvalid = ftpupload.Connect();
            if (!isvalid)
            {
                message = ftpupload.errormessage;
                return isvalid;
            }

            //切换到指定路径下,如果目录不存在,将创建
            if (!ftpupload.ChangeDir(uploadpath))
            {
                ftpupload.MakeDir(uploadpath);
                if (!ftpupload.ChangeDir(uploadpath))
                {
                    message += ftpupload.errormessage;
                    isvalid = false;
                }
            }            
            return isvalid;
        }
示例#3
0
文件: FTPs.cs 项目: ichari/ichari
        /// <summary>
        /// 普通FTP上传文件
        /// </summary>
        /// <param name="file">要FTP上传的文件</param>
        /// <returns>上传是否成功</returns>
        public bool UpLoadFile(string path, string file, FTPUploadEnum ftpuploadname)
        {
            FTP ftpupload = new FTP();
            //转换路径分割符为"/"
            path = path.Replace("\\", "/");
            path = path.StartsWith("/") ? path : "/" +path ;

            //删除file参数文件
            bool delfile = true;
            //根据上传名称确定上传的FTP服务器
            switch (ftpuploadname)
            {
                //论坛附件
                case FTPUploadEnum.ForumAttach:
                    {
                        ftpupload = new FTP(m_forumattach.Serveraddress, m_forumattach.Serverport, m_forumattach.Username, m_forumattach.Password, m_forumattach.Timeout);
                        path = m_forumattach.Uploadpath + path;
                        delfile = (m_forumattach.Reservelocalattach == 1) ? false : true;
                        break;
                    }

                //空间附件
                case FTPUploadEnum.SpaceAttach:
                    {
                        ftpupload = new FTP(m_spaceattach.Serveraddress, m_spaceattach.Serverport, m_spaceattach.Username, m_spaceattach.Password, m_spaceattach.Timeout);
                        path = m_spaceattach.Uploadpath + path;
                        delfile = (m_spaceattach.Reservelocalattach == 1) ? false : true;
                        break;
                    }

                //相册附件
                case FTPUploadEnum.AlbumAttach:
                    {
                        ftpupload = new FTP(m_albumattach.Serveraddress, m_albumattach.Serverport, m_albumattach.Username, m_albumattach.Password, m_albumattach.Timeout);
                        path = m_albumattach.Uploadpath + path;
                        delfile = (m_albumattach.Reservelocalattach == 1) ? false : true;
                        break;
                    }
                //商城附件
                case FTPUploadEnum.MallAttach:
                    {
                        ftpupload = new FTP(m_mallattach.Serveraddress, m_mallattach.Serverport, m_mallattach.Username, m_mallattach.Password, m_mallattach.Timeout);
                        path = m_mallattach.Uploadpath + path;
                        delfile = (m_mallattach.Reservelocalattach == 1) ? false : true;
                        break;
                    }
            }

            //切换到指定路径下,如果目录不存在,将创建
            if (!ftpupload.ChangeDir(path))
            {
                //ftpupload.MakeDir(path);
                foreach (string pathstr in path.Split('/'))
                {
                    if (pathstr.Trim() != "")
                    {
                        ftpupload.MakeDir(pathstr);
                        ftpupload.ChangeDir(pathstr);
                    }
                }
                
            }
            
            ftpupload.Connect();

            if (!ftpupload.IsConnected)
            {
                return false;
            }
            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();

            //(如存在)删除指定目录下的文件
            if (delfile && Utils.FileExists(file))
            {
                System.IO.File.Delete(file);
            }
          

            if (perc >= 100)
            {
                return true;
            }
            else
            {
                return false;
            }
        }