Public byte[] Ftpdownload (string filePath) {try {ftpwebrequest reqftp = Get FtpWebRequest (New Uri (fptserver + Ftpmappath + filePath), WebRequestMethods.Ftp.DownloadFile); FtpWebResponse response = (ftpwebresponse) reqftp.getresponse (); if (response = = NULL) {return null; } Stream Retstream = response. GetResponseStream (); list<byte> Contentarray = new list<byte> (); int bt = Retstream.readbyte (); while (BT >-1) {Contentarray.add ((byte) bt); BT = Retstream.readbyte (); } retstream.dispose (); return Contentarray.toarray (); } catch (Exception ex) {Loghelper.logerror ("Ftpdownload failed! ", ex); return null; }} PrivaTe ftpwebrequest getftpwebrequest (Uri Uri, string method) {FtpWebRequest ftpclientrequest = (ftpwebrequ EST) webrequest.create (URI); Ftpclientrequest.proxy = null; Ftpclientrequest.credentials = new NetworkCredential (Ftpuserid, FTPPassword); Ftpclientrequest.usebinary = true; Ftpclientrequest.usepassive = true; Ftpclientrequest.method = Method; Ftpclientrequest.timeout =-1; return ftpclientrequest; } private bool Ftpuploadfile (byte[] filecontent,string fullPath) {try { String ftpfiledir = Path.getdirectoryname (FullPath); This always executes Ftpmakedirbypath (ftpfiledir.replace ("\ \", "/")); FtpWebRequest reqftp = getftpwebrequest (new Uri (Fptserver+fullpath), WebRequestMethods.Ftp.UploadFile); Stream stream = Reqftp.getrequeststream (); Stream. Write (filecontent, 0, FileconTent. Length); Stream. Flush (); Stream. Close (); return true; } catch (Exception ex) {Loghelper.logerror ("Ftpuploadfile failed! ", ex); return false; }}////<summary>//Create a multilevel directory based on the path///</summary>//<param N Ame= "Dirpath" ></param>///<returns></returns> private bool Ftpmakedirbypath (String dir Path) {if (string. IsNullOrEmpty (Dirpath)) {return false; } dirpath=dirpath.replace ("\ \", "/"); String[] dirs= dirpath.split (new char[] {'/'}, Stringsplitoptions.removeemptyentries); if (dirs! = null && dirs. Length > 0) {bool result = false; String des = fptserver; try {for (int i = 0; i < dirs. LengTh i++) {result = Ftpmakedir (DES, dirs[i]); des = des + "/" + dirs[i]; } return true; } catch (Exception ex) {Loghelper.logerror ("Ftpmakedirbypath failed! ", ex); return false; }} return false; }//Create directory private bool Ftpmakedir (string destinationpath,string dirName) {ftpwebresponse R Esponse = null; try {uri uri = new Uri (destinationpath+ "/" + dirName); FtpWebRequest reqftp = Getftpwebrequest (URI, WebRequestMethods.Ftp.MakeDirectory); Response = (FtpWebResponse) reqftp.getresponse (); Response. Close (); return true; } catch (Exception ex) {Loghelper.logerror ("Ftpmakedir failed! ", Ex); return false; } }
ASP. NET uploads files via FTP