示例#1
0
        public static string PutFileOnFtpServer(FileInfo file, string newName, FlexiCapture.Cloud.FTPService.Models.FTPSetting setting, string pathToPut)
        {
            try
            {
                // CONVERSION SETING MODEL!!!
                FlexiCapture.Cloud.Portal.Api.Models.SettingsModels.FTPConversionSettingModel conversionSetting =
                    FlexiCapture.Cloud.Portal.Api.
                    Helpers.FtpConversionSettingsHelpers.
                    FtpConversionSettingsHelper.GetSettingsByUserId(setting.UserId);


                string bbaseUri  = "ftp://" + setting.Host;
                string uri       = Path.Combine(pathToPut, newName);
                Uri    baseUri   = new Uri(bbaseUri);
                Uri    serverUri = new Uri(baseUri, uri);
                if (serverUri.Scheme != Uri.UriSchemeFtp)
                {
                    return("");
                }

                CheckPathToPut(pathToPut, setting);

                FtpWebRequest ftpClient;
                ftpClient             = (FtpWebRequest)FtpWebRequest.Create(serverUri.AbsoluteUri);
                ftpClient.Credentials = new System.Net.NetworkCredential(setting.UserName, PasswordHelper.Crypt.DecryptString(setting.Password));
                ftpClient.Method      = System.Net.WebRequestMethods.Ftp.UploadFile;
                ftpClient.UseBinary   = true;
                ftpClient.KeepAlive   = true;

                ftpClient.ContentLength = file.Length;
                byte[] buffer           = new byte[4097];
                int    bytes            = 0;
                int    total_bytes      = (int)file.Length;
                System.IO.FileStream fs = file.OpenRead();
                System.IO.Stream     rs = ftpClient.GetRequestStream();
                while (total_bytes > 0)
                {
                    bytes = fs.Read(buffer, 0, buffer.Length);
                    rs.Write(buffer, 0, bytes);
                    total_bytes = total_bytes - bytes;
                }
                //fs.Flush();
                fs.Close();
                rs.Close();
                FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
                var            value          = uploadResponse.StatusDescription;
                uploadResponse.Close();

                return(value);
            }
            catch (Exception ex)
            {
                string innerException = ex.InnerException == null ? "" : ex.InnerException.Message;
                string methodName     = System.Reflection.MethodBase.GetCurrentMethod().Name;
                LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + ex.Message + " Innner Exception: " +
                                 innerException);
                return("");
            }
        }
示例#2
0
        private static bool CheckPathToPut(string path, FlexiCapture.Cloud.FTPService.Models.FTPSetting setting)
        {
            string bbaseUri  = "ftp://" + setting.Host;
            string uri       = "";
            Uri    baseUri   = new Uri(bbaseUri);
            Uri    serverUri = new Uri(baseUri, uri);


            string[] pathFolders = path.Split('/');



            foreach (var pathItem in pathFolders)
            {
                try
                {
                    if (string.IsNullOrEmpty(pathItem) ||
                        string.IsNullOrWhiteSpace(pathItem))
                    {
                        continue;
                    }

                    uri = Path.Combine(uri, "/", pathItem);

                    serverUri = new Uri(baseUri, uri);

                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
                    request.Method      = WebRequestMethods.Ftp.MakeDirectory;
                    request.Credentials = new NetworkCredential(setting.UserName,
                                                                PasswordHelper.Crypt.DecryptString(setting.Password));

                    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                    {
                        var st = response.StatusCode;
                    }
                }
                catch (WebException ex)
                {
                    continue;
                    //if (ex.Response != null)
                    //{
                    //    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
                    //    request.Method = WebRequestMethods.Ftp.MakeDirectory;
                    //    request.Credentials = new NetworkCredential(setting.UserName,
                    //        PasswordHelper.Crypt.DecryptString(setting.Password));
                    //    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                    //    {
                    //        if (response.StatusCode != FtpStatusCode.CommandOK)
                    //        {
                    //            return false;
                    //        }
                    //        else
                    //        {
                    //            CheckPathToPut(path, setting);
                    //        }
                    //    }
                    //}
                }


                return(true);
            }

            return(true);
        }