コード例 #1
0
ファイル: WebUtils.cs プロジェクト: Robin--/RMLib
        static public bool FtpUpload(bool useSsl, string hostName, string userName, RMSecureString password, string remoteDirectory, string fileName, EventHandler <FtpUploadProgressEventArgs> progressEventHandler)
        {
            FtpWebRequest ftpRequest = null;

            try
            {
                ftpRequest             = (FtpWebRequest)WebRequest.Create("ftp://" + hostName + remoteDirectory + Path.GetFileName(fileName));
                ftpRequest.Method      = WebRequestMethods.Ftp.UploadFile;
                ftpRequest.Proxy       = null;
                ftpRequest.UseBinary   = true;
                ftpRequest.UsePassive  = true;
                ftpRequest.Credentials = new NetworkCredential(userName, password.GetPlainText());
                ftpRequest.KeepAlive   = false;
                //ftpRequest.EnableSsl = ASSL;
                //if (ASSL) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(FtpUploadCertificateValidation);

                FileInfo FI = new FileInfo(fileName);
                using (Stream Reader = FI.OpenRead())
                {
                    using (Stream Writer = ftpRequest.GetRequestStream())
                    {
                        FtpUploadProgressEventArgs e = new FtpUploadProgressEventArgs(FI.Length);
                        byte[] Buffer         = new byte[8192];
                        long   TotalBytesRead = 0;

                        progressEventHandler(null, e);
                        int BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        while (BytesRead > 0)
                        {
                            TotalBytesRead += BytesRead;

                            Writer.Write(Buffer, 0, BytesRead);
                            e.BytesSent = TotalBytesRead;
                            progressEventHandler(null, e);

                            BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        }
                    }
                }
                ftpRequest.GetResponse().Close();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                ftpRequest = null;
            }
        }
コード例 #2
0
ファイル: WebUtils.cs プロジェクト: Robin--/RMLib
        public static bool FtpUpload(bool useSsl, string hostName, string userName, RMSecureString password, string remoteDirectory, string fileName, EventHandler<FtpUploadProgressEventArgs> progressEventHandler)
        {
            FtpWebRequest ftpRequest = null;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + hostName + remoteDirectory + Path.GetFileName(fileName));
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                ftpRequest.Proxy = null;
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.Credentials = new NetworkCredential(userName, password.GetPlainText());
                ftpRequest.KeepAlive = false;
                //ftpRequest.EnableSsl = ASSL;
                //if (ASSL) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(FtpUploadCertificateValidation);

                FileInfo FI = new FileInfo(fileName);
                using (Stream Reader = FI.OpenRead())
                {
                    using (Stream Writer = ftpRequest.GetRequestStream())
                    {
                        FtpUploadProgressEventArgs e = new FtpUploadProgressEventArgs(FI.Length);
                        byte[] Buffer = new byte[8192];
                        long TotalBytesRead = 0;

                        progressEventHandler(null, e);
                        int BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        while (BytesRead > 0)
                        {
                            TotalBytesRead += BytesRead;

                            Writer.Write(Buffer, 0, BytesRead);
                            e.BytesSent = TotalBytesRead;
                            progressEventHandler(null, e);

                            BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        }
                    }
                }
                ftpRequest.GetResponse().Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                ftpRequest = null;
            }
        }