Esempio n. 1
0
        /// <summary>
        /// 检测文件夹是否存在
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(检测文件夹路径)</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        /// <returns>检测情况</returns>
        public static EExistence CF_IsExistDirectory(ModFtpInfomation ftpInfomation, out string errMsg)
        {
            EExistence result;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                string dirname = ftpInfomation.GetCurrentName();
                ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetParentPath();

                result = CF_IsExistDirectory(ftpInfomation, dirname, out errMsg);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = EExistence.Error;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(目标文件路径)</param>
        /// <param name="fileName">FTP文件名</param>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="overwrite">是否覆盖文件</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        public static bool CF_UploadFile(ModFtpInfomation ftpInfomation, string fileName, string filePath, bool overwrite, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                //判断原始文件是否存在
                FileInfo file = new FileInfo(filePath);
                if (!file.Exists)
                {
                    errMsg = "本地文件不存在!";
                    result = false;
                }
                else
                {
                    EExistence isExistDir = CF_IsExistDirectory(ftpInfomation, out errMsg);
                    if (isExistDir == EExistence.Exist || string.IsNullOrEmpty(ftpInfomation.GetCurrentName()))
                    {
                        //判断目标文件是否存在
                        EExistence isExistFile = CF_IsExistFile(ftpInfomation, fileName, out errMsg);
                        if (isExistFile == EExistence.Exist)
                        {
                            //是否覆盖
                            if (!overwrite)
                            {
                                errMsg = "目标文件已存在!";
                                result = false;
                            }
                            else
                            {
                                isExistFile = EExistence.NotExist;
                            }
                        }

                        if (isExistFile == EExistence.NotExist)
                        {
                            ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.UploadFile;
                            ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(fileName);
                            FtpWebRequest ftpWebRequest = CF_GetFtpRequest(ftpInfomation, out errMsg);

                            if (ftpWebRequest != null)
                            {
                                using (Stream stream = ftpWebRequest.GetRequestStream())
                                {
                                    using (FileStream fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                                    {
                                        ModFtpTransSpeed transmissionSpeed = ftpInfomation.FtpReqInfo.UploadSpeed;
                                        if (transmissionSpeed.EnableLimit)
                                        {
                                            //缓存字节数
                                            int bufferSize = transmissionSpeed.Speed * (int)Math.Pow(2, transmissionSpeed.Unit.CF_ToNumber());
                                            //缓存
                                            byte[] btBuffer = new byte[bufferSize];

                                            //读取的字节数
                                            int readSize = fileStream.Read(btBuffer, 0, bufferSize);
                                            while (readSize > 0)
                                            {
                                                //写入本地文件
                                                stream.Write(btBuffer, 0, readSize);

                                                readSize = fileStream.Read(btBuffer, 0, bufferSize);

                                                //延时
                                                if (readSize > 0)
                                                {
                                                    Thread.Sleep(transmissionSpeed.Delay);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            fileStream.CopyTo(stream);
                                        }
                                    }
                                }
                            }

                            result = true;
                        }
                    }
                    else
                    {
                        errMsg = "目标文件夹不存在!";
                        result = false;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }