public StorageReturnValue StorageFtpDelete(string pathFileNameWithExt) { StorageReturnValue result = new StorageReturnValue(false, FileStorageProperties.GetInstance.WrongInitialManagement, null); //Determine if file or full path string URI = this.Hostname + GetFullPath(pathFileNameWithExt); System.Net.FtpWebRequest ftp = GetRequest(URI); //Set request to delete ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; try { //get response but ignore it string str = GetStringResponse(ftp); result = new StorageReturnValue(true, string.Empty, null); } catch (Exception ex) { result = new StorageReturnValue(true, ex.Message, null); } return(result); }
public StorageReturnValue StorageDownload(string pathFileNameWithExt) { StorageReturnValue result = new StorageReturnValue(false, FileStorageProperties.GetInstance.WrongInitialManagement, null); //2. check source string target = pathFileNameWithExt; if (target.Trim() == "") { throw (new ApplicationException("File not specified")); } else if (target.Contains("/")) { //treat as a full path target = AdjustDir(target); } else { //treat as filename only, use current directory target = CurrentDirectory + target; } string URI = Hostname + target; //3. perform copy System.Net.FtpWebRequest ftp = GetRequest(URI); //Set request to download a file in binary mode ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile; ftp.UseBinary = true; string tempFolder = this.FolderTempGenerated(); string tempFileName = Guid.NewGuid().ToString() + Path.GetExtension(pathFileNameWithExt); string tempFileDownload = Path.Combine(tempFolder, tempFileName); FileStream outputStream = new FileStream(tempFileDownload, FileMode.Create); //open request and get response stream using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { //loop to read & write to file try { byte[] buffer = new byte[2048]; int read = 0; do { read = responseStream.Read(buffer, 0, buffer.Length); outputStream.Write(buffer, 0, read); } while (!(read == 0)); responseStream.Close(); outputStream.Flush(); outputStream.Close(); } catch (Exception) { //catch error and delete file only partially downloaded outputStream.Close(); } responseStream.Close(); } response.Close(); } byte[] resultx = null; if (outputStream != null) { outputStream.Close(); } if (File.Exists(tempFileDownload)) { resultx = File.ReadAllBytes(tempFileDownload); File.Delete(tempFileDownload); } if (resultx != null) { string filename = Path.GetFileName(pathFileNameWithExt); result = result.SetStorageReturnValue(true, string.Empty, new List <FileStorageAttachment>() { new FileStorageAttachment(filename, filename, resultx) }); } else { string filename = Path.GetFileName(pathFileNameWithExt); result = result.SetStorageReturnValue(true, "Error download", null); } return(result); }
public StorageReturnValue StorageUpload(string pathFileNameWithExt, byte[] uploadedFile) { //copy the file specified to target file: target file can be full path or just filename (uses current dir) //copy the file specified to target file: target file can be full path or just filename (uses current dir) StorageReturnValue result = new StorageReturnValue(false, FileStorageProperties.GetInstance.WrongInitialManagement, null); //1. check target try { string target; if (pathFileNameWithExt.Trim() == "") { //Blank target: use source filename & current dir target = this.CurrentDirectory + pathFileNameWithExt; } else if (pathFileNameWithExt.Contains("/")) { //If contains / treat as a full path target = AdjustDir(pathFileNameWithExt); } else { //otherwise treat as filename only, use current directory target = CurrentDirectory + pathFileNameWithExt; } string URI = Hostname + target; //perform copy System.Net.FtpWebRequest ftp = GetRequest(URI); //Set request to upload a file in binary ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; ftp.UseBinary = true; //Notify FTP of the expected size ftp.ContentLength = uploadedFile.Length; //create byte array to store: ensure at least 1 byte! const int BufferSize = 2048; byte[] content = new byte[BufferSize - 1 + 1]; int total_bytes = (int)uploadedFile.Length; using (Stream rs = ftp.GetRequestStream()) { while (total_bytes < uploadedFile.Length) { rs.Write(uploadedFile, total_bytes, BufferSize); total_bytes += BufferSize; } rs.Close(); } result = result.SetStorageReturnValue(true, string.Empty, null); } catch (Exception ex) { result = result.SetStorageReturnValue(false, ex.Message.ToString(), null); } return(result); }