<summary>//FTP upload File///</summary>//<param name= "fileUpload" > Upload control </para m>//<param name= "Ftpserverip" > Upload file Server ip</param>//<param name= "Ftpuserid" > Server user name &L t;/param>//<param name= "FTPPassword" > Server password </param>//<returns></returns> public string Upload (FileUpload FileUpload, String Ftpserverip, String Ftpuserid, String FTPPassword) { string filename = Fileupload.filename; String Sret = "Upload succeeded!" "; FileInfo Fileinf = new FileInfo (fileUpload.PostedFile.FileName); String uri = "ftp://" + Ftpserverip + "/" + filename; FtpWebRequest reqftp; Creates a FtpWebRequest object based on a Uri reqftp = (ftpwebrequest) ftpwebrequest.create (new Uri (URI)); FTP user name and password reqftp.credentials = new NetworkCredential (Ftpuserid, FTPPassword); The default is true, the connection is not closed//in a lifeAfter the order is executed reqftp.keepalive = false; Specifies what command to execute Reqftp.method = WebRequestMethods.Ftp.UploadFile; Specifies the data transfer type reqftp.usebinary = TRUE; Reqftp.usepassive = false; Notifies server file size when uploading a file Reqftp.contentlength = Fileinf.length; The buffer size is set to 2kb int bufflength = 2048; byte[] buff = new Byte[bufflength]; int Contentlen; Open a file stream (System.IO.FileStream) to read the uploaded file FileStream fs = Fileinf.openread (); try {//writes the uploaded file to stream stream strm = Reqftp.getrequeststream (); 2KB contentlen per read file stream = fs. Read (Buff, 0, bufflength); Stream content does not end while (Contentlen! = 0) {//writes content from file stream to upload stream Strm. Write (Buff, 0, Contentlen); Contentlen = fs. Read (Buff, 0, bufflength); }//Close two stream strm. Close (); Fs. Close (); } catch (Exception ex) {Sret = ex. Message; } return Sret; }
<summary>//FTP download File///</summary>//<param name= "userId" >ftp username </param& Gt <param name= "pwd" >ftp password </param>//<param name= "ftppath" >ftp file path </param>//&L T;param name= "FilePath" > Download save path </param>//<param name= "filename" >ftp file name </param>//&L T;returns></returns> public string Download (string userId, string pwd, String Ftppath, string FilePath, S Tring fileName) {string sret = "Download successful! "; FtpWebRequest reqftp; try {FileStream outputstream = new FileStream (FilePath + fileName, filemode.create); Creates a FtpWebRequest object based on a Uri reqftp = (ftpwebrequest) ftpwebrequest.create (new Uri (Ftppath + fileName) ); Specifies what command to execute Reqftp.method = WebRequestMethods.Ftp.DownloadFile; Specify the data Transfer type Reqftp.useBinary = true; Reqftp.usepassive = false; FTP user name and password reqftp.credentials = new NetworkCredential (userId, PWD); FtpWebResponse response = (ftpwebresponse) reqftp.getresponse (); Write the downloaded file to stream ftpstream = response. GetResponseStream (); Long cl = Response. ContentLength; The buffer size is set to 2kb int buffersize = 2048; int readcount; byte[] buffer = new Byte[buffersize]; 2KB readcount per read file stream = ftpstream.read (buffer, 0, buffersize); while (Readcount > 0) {//writes the content from the file stream to Outputstream.write (buffer, 0, Readcount); Readcount = ftpstream.read (buffer, 0, buffersize); }//Close two streams and FTP connection ftpstream.close (); Outputstream.close (); REsponse. Close (); } catch (Exception ex) {Sret=ex. Message; }//returns the download result (whether the download was successful) return Sret; }
<summary>//FTP Delete files///</summary>//<param name= "ftppath" >ftp file path </para m>//<param name= "userId" >ftp user name </param>///<param name= "pwd" >ftp password </param> <param name= "filename" >ftp filename </param>///<returns></returns> public string DeleteFile (String ftppath,string userid,string pwd, String fileName) {string Sret = "Delete succeeded! "; FtpWebResponse respose = null; FtpWebRequest reqftp = null; Stream localfile = null; Stream stream = null; try {//Create FtpWebRequest object based on URI reqftp = (ftpwebrequest) ftpwebrequest.create (new Uri (String. Format (@ "{0}{1}", Ftppath, FileName)); Provide verification of account password reqftp.credentials = new NetworkCredential (userId, PWD); The default is true to not close the FTP connection after uploading reqftp.keepalive = FALse Perform the delete operation Reqftp.method = WebRequestMethods.Ftp.DeleteFile; Respose = (ftpwebresponse) reqftp.getresponse (); } catch (Exception ex) {Sret = ex. Message; } finally {//close connection with stream if (respose! = null) respose.c Lose (); if (localfile! = null) LocalFile. Close (); if (stream! = null) stream. Close (); }//Returns execution status return Sret; }
<summary>//FTP Create directory///</summary>//<param name= "dirName" > directory name </PARAM&G T <param name= "Ftpserverip" > Server address </param>//<param name= "Ftpuserid" >ftp user name </param> <param name= "FTPPassword" >ftp password </param>///<returns></returns> public Strin G Createdir (String dirName, String Ftpserverip, String Ftpuserid, String FTPPassword) {string Sret = "O K "; try {string uri = Ftpserverip + "/" + dirName; FtpWebRequest reqftp; Creates a FtpWebRequest object based on a Uri reqftp = (ftpwebrequest) ftpwebrequest.create (new Uri (URI)); FTP user name and password reqftp.credentials = new NetworkCredential (Ftpuserid, FTPPassword); The default is true, the connection is not closed//After a command is executed reqftp.keepalive = false; Specify what commands to execute Reqftp.method = WebRequestMethods.Ftp.MakeDirectory; Specifies the data transfer type reqftp.usebinary = TRUE; FtpWebResponse respftp = (ftpwebresponse) reqftp.getresponse (); Respftp.close (); } catch (Exception ex) {Sret = ex. Message; } return Sret; }
FTP file operations upload text, download files, delete files, create directories