public static bool FtpDirectoryNotFound(PLRAttachmentVM PLRAttachmentVM, string currentDir) { bool IsDirectoryNotFound = false; FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(currentDir); ftpRequest.Credentials = new NetworkCredential(PLRAttachmentVM.FtpUsername, PLRAttachmentVM.FtpPassword); ftpRequest.KeepAlive = true; try { ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) { using (Stream ftpStream = response.GetResponseStream()) { ftpStream.Close(); } response.Close(); } } catch (WebException ex) { if (ex.Response != null) { FtpWebResponse response = (FtpWebResponse)ex.Response; // Directory not found. IsDirectoryNotFound = response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable || response.StatusCode == FtpStatusCode.NotLoggedIn; } } return(IsDirectoryNotFound); }
public static string UploadFileToFtp(PLRAttachmentVM PLRAttachmentVM) { string ftpResult = string.Empty; byte[] fileData = null; FtpWebRequest ftpRequest = null; try { for (int i = 0; i < PLRAttachmentVM.Request.Files.Count; i++) { string currentDir = $"{PLRAttachmentVM.FtpUrl}/{PLRAttachmentVM.ClaimId}/{PLRAttachmentVM.FolderName.ToLowerInvariant()}/"; currentDir = $"{currentDir}{PLRAttachmentVM.Request.Files[i].FileName.ToLowerInvariant()}"; ftpRequest = (FtpWebRequest)WebRequest.Create(currentDir); ftpRequest.UseBinary = true; ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpRequest.Credentials = new NetworkCredential(PLRAttachmentVM.FtpUsername, PLRAttachmentVM.FtpPassword); ftpRequest.Proxy = null; ftpRequest.KeepAlive = true; using (BinaryReader binaryReader = new BinaryReader(PLRAttachmentVM.Request.Files[i].InputStream)) { fileData = binaryReader.ReadBytes(PLRAttachmentVM.Request.Files[i].ContentLength); } ftpRequest.ContentLength = fileData.Length; using (Stream requestStream = ftpRequest.GetRequestStream()) { requestStream.Write(fileData, 0, fileData.Length); } using (FtpWebResponse ftpResp = (FtpWebResponse)ftpRequest.GetResponse()) { if (ftpResp != null && ftpResp.StatusDescription.StartsWith("226", StringComparison.Ordinal)) { PLRDAO.SaveUploadInformation(PLRAttachmentVM, i); ftpResult = ValidationMessages.FileUploadComplete; } } } } catch (Exception ex) { LoggerService.LogException(ex); } return(ftpResult); }
public static string CreateFtpDirectory(PLRAttachmentVM PLRAttachmentVM) { string ftpResult = string.Empty; try { FtpWebRequest reqFTP = null; Stream ftpStream = null; string[] subDirs = { PLRAttachmentVM.ClaimId, PLRAttachmentVM.FolderName.ToLowerInvariant() }; string currentDir = PLRAttachmentVM.FtpUrl; foreach (string subDir in subDirs) { currentDir = $"{currentDir}/{subDir}"; reqFTP = (FtpWebRequest)WebRequest.Create(currentDir); reqFTP.Credentials = new NetworkCredential(PLRAttachmentVM.FtpUsername, PLRAttachmentVM.FtpPassword); reqFTP.KeepAlive = true; if (FtpDirectoryNotFound(PLRAttachmentVM, currentDir)) { // create directory if it doesn't exist reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; reqFTP.UseBinary = true; using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) { ftpStream = response.GetResponseStream(); ftpStream.Close(); response.Close(); } } } } catch (WebException e) { if (e.Message == ValidationMessages.RemoteNotLoggedin) { ftpResult = ValidationMessages.NotLoggedIn; } else { ftpResult = ValidationMessages.CreateDirectoryFailed; } } return(ftpResult); }
public Response <List <UploadImageStatus> > SaveAttachment(string folderName, string claimId, int?userId, HttpRequestBase Request, FtpCredentials ftpCredentials) { string ftpResult = string.Empty; string pathToCreate = $"{claimId}/{folderName}"; List <UploadImageStatus> lstimgstatus = new List <UploadImageStatus>(); Response <List <UploadImageStatus> > response = new Response <List <UploadImageStatus> >(); PLRAttachmentVM PLRAttachmentVM = new PLRAttachmentVM { FtpUrl = ftpCredentials.FTPAddress, PathToCreate = pathToCreate, FtpUsername = ftpCredentials.FTPUserName, FtpPassword = ftpCredentials.FTPPassword, ClaimId = claimId, FolderName = folderName, Status = folderName.Substring(0, 1).ToUpper(), UserId = userId.ToString(), Request = Request }; try { ftpResult = FtpIOExtensions.CreateFtpDirectory(PLRAttachmentVM); if (string.IsNullOrEmpty(ftpResult)) { ftpResult = FtpIOExtensions.UploadFileToFtp(PLRAttachmentVM); lstimgstatus = PLRDAO.UploadedImagesDetail(PLRAttachmentVM.ClaimId); response.Status = ResultStatus.Success; response.Message = "Success"; response.Data = lstimgstatus; } } catch (Exception ex) { LoggerService.LogException(ex); response.Status = ResultStatus.Error; response.Message = "Error"; response.Data = lstimgstatus; } return(response); }