コード例 #1
0
ファイル: FTPGroup.cs プロジェクト: migeyusu/FORCEBuilds
        public static void DownLoadAbsoluteFile(Stream stream, string path, RateDelegate rateCallBack = null)
        {
            long fileLength = 0;

            if (rateCallBack != null)
            {
                var request = (FtpWebRequest)WebRequest.Create(path);
                request.Method    = WebRequestMethods.Ftp.GetFileSize;
                request.UseBinary = true;
                using (var ftpWebResponse = (FtpWebResponse)request.GetResponse()) {
                    fileLength = ftpWebResponse.ContentLength;
                }
            }
            var ftpWebRequest = (FtpWebRequest)WebRequest.Create(path);

            ftpWebRequest.Method     = WebRequestMethods.Ftp.DownloadFile;
            ftpWebRequest.UseBinary  = true;
            ftpWebRequest.UsePassive = false;
            using (var response = (FtpWebResponse)ftpWebRequest.GetResponse()) {
                using (var responseStream = response.GetResponseStream()) {
                    var  buffer = new byte[BufferSize];
                    int  read;
                    long position = 0;
                    do
                    {
                        read      = responseStream.Read(buffer, 0, buffer.Length);
                        position += read;
                        rateCallBack?.Invoke(position, fileLength);
                        stream.Write(buffer, 0, read);
                    } while (read != 0);
                }
            }
        }
コード例 #2
0
ファイル: FTPGroup.cs プロジェクト: migeyusu/FORCEBuilds
        /// <summary>
        /// 上传内存流
        /// </summary>
        /// <param name="path"></param>
        /// <param name="stream"></param>
        /// <param name="length"></param>
        /// <param name="rateCallback"></param>
        public static void UploadStreamAbsolute(string path, Stream stream, long length = 0,
                                                RateDelegate rateCallback = null)
        {
            var request = (FtpWebRequest)WebRequest.Create(path);

            request.Method        = WebRequestMethods.Ftp.UploadFile;
            request.UseBinary     = true;
            request.UsePassive    = true;
            request.ContentLength = stream.Length;
            var content = new byte[BufferSize - 1 + 1];

            stream.Position = 0;
            try {
                using (var requestStream = request.GetRequestStream())
                {
                    long position = 0;
                    int  dataRead;
                    do
                    {
                        dataRead  = stream.Read(content, 0, BufferSize);
                        position += dataRead;
                        rateCallback?.Invoke(position, length);
                        requestStream.Write(content, 0, dataRead);
                    } while (!(dataRead < BufferSize));
                }
            }
            catch (Exception) {
                DeleteAbsoluteFile(path);
                throw;
            }
        }