FTP Upload Class

Source: Internet
Author: User
Tags ftp login
<span id="Label3"></p><p><p>Using System;<br>Using System.Collections.Generic;<br>Using system.io;<br>Using system.linq;<br>Using system.net;<br>Using system.text;<br>Using system.web;</p></p><p>Namespace Ftphelper<br>{<br>This article is contributed by moscms<br>public class Ftp_class<br>{<br>private String ftpserverip;<br>private String ftpuserid;<br>private String ftppassword;<br>FtpWebRequest reqftp;<br>public void Connecttest (string ftpserverip, string ftpuserid, string Ftppassword)<br>{<br>To create a FtpWebRequest object from a URI<br>Reqftp = (ftpwebrequest) ftpwebrequest.create (new Uri ("ftp://" + ftpserverip));<br>Specifying data Transfer Types<br>Reqftp.usebinary = true;<br>Ftpuserid = ftpuserid;<br>FTPPassword = ftppassword;<br>FTP User name and password<br>Reqftp.credentials = new NetworkCredential (ftpuserid, ftppassword);<br> <br>}<br>#region Connection<br>private void Connect (String path)//connect FTP<br>{<br>To create a FtpWebRequest object from a URI<br>Reqftp = (ftpwebrequest) ftpwebrequest.create (new Uri (path));<br>Specifying data Transfer Types<br>Reqftp.usebinary = true;<br>FTP User name and password<br>Reqftp.credentials = new NetworkCredential (ftpuserid, ftppassword);<br>}<br>#endregion<br>#region FTP Login Information<br>public void Ftpupdown (string ftpserverip, string ftpuserid, string Ftppassword)<br>{<br>This.ftpserverip = ftpserverip;<br>This.ftpuserid = ftpuserid;<br>This.ftppassword = ftppassword;<br>}<br>#endregion<br>#region get a list of files</p><p>Get a list of files<br>Private string[] getfilelist (string path, string wrmethods)//the above code example how to get a list of files from an FTP server<br>{<br>string[] downloadfiles;<br>StringBuilder result = new StringBuilder ();<br>Try<br>{<br>Connect (path);<br>Reqftp.method = wrmethods;<br>WebResponse response = Reqftp.getresponse ();<br>StreamReader reader = new StreamReader (response. GetResponseStream (), System.Text.Encoding.UTF8);//chinese file name<br>String line = Reader. ReadLine ();<br>While (line! = Null)<br>{<br>Result. Append (line);<br>Result. Append ("\ n");<br>line = Reader. ReadLine ();<br>}<br>To remove the trailing ' \ n '<br>Result. Remove (result. ToString (). LastIndexOf (' \ n '), 1);<br>Reader. Close ();<br>Response. Close ();<br>Return Result. ToString (). Split (' \ n ');<br>}<br>Catch (Exception Ex)<br>{<br>System.Windows.Forms.MessageBox.Show (ex. Message);<br>Downloadfiles = null;<br>Return downloadfiles;<br>}<br>}<br>Public string[] getfilelist (string path)//the above code example how to get a list of files from an FTP server<br>{<br>return getfilelist ("ftp://" + ftpserverip + "/" + path, WebRequestMethods.Ftp.ListDirectory);<br>}<br>Public string[] getfilelist ()//the above code example how to get a list of files from an FTP server<br>{<br>return getfilelist ("ftp://" + ftpserverip + "/", WebRequestMethods.Ftp.ListDirectory);<br>}<br>#endregion</p><p><p>#region Uploading Files<br>public bool Upload (string filename, string path, out string Errorinfo)//the code above implements the ability to upload files from an FTP server<br>{<br>Path = Path. Replace ("\ \", "/");<br>FileInfo fileinf = new FileInfo (filename);<br>String uri = "ftp://" + path + "/" + fileinf.name;<br>Connect (uri);//connect<br>The default is true and the connection is not closed<br>is executed after a command<br>Reqftp.keepalive = false;<br>Specify what commands to execute</p></p><p>Reqftp.method = WebRequestMethods.Ftp.UploadFile;<br>Notifies server file size when uploading a file<br>Reqftp.contentlength = fileinf.length;<br>Buffer size set to KB<br>int bufflength = 2048;<br>byte[] buff = new byte[bufflength];<br>int contentlen;<br>Open a file stream (System.IO.FileStream) to read the uploaded file<br>FileStream fs = Fileinf.openread ();<br>Try<br>{<br>Write the uploaded file to the stream<br>Stream STRM = Reqftp.getrequeststream ();<br>KB per read file stream<br>Contentlen = Fs. Read (buff, 0, bufflength);<br>Stream content does not end<br>While (contentlen! = 0)<br>{<br>Write content from file stream to upload stream<br>Strm. Write (buff, 0, contentlen);<br>Contentlen = Fs. Read (buff, 0, bufflength);<br>}<br>Close two streams<br>Strm. Close ();<br>Fs. Close ();<br>ErrorInfo = "done";<br>Return true;<br>}<br>Catch (Exception Ex)<br>{<br>ErrorInfo = String. Format ("unable to complete upload because {0}", ex. Message);<br>Return false;<br>}<br>}<br>#endregion<br>#region Continuation of documents<br>public bool Upload (string filename, Long size, string path, out string Errorinfo)//the code above implements the ability to upload files from an FTP server<br>{<br>Path = Path. Replace ("\ \", "/");<br>FileInfo fileinf = new FileInfo (filename);<br>String uri = "ftp://" + path + "/" + fileinf.name;<br>String uri = "ftp://" + path;<br>Connect (uri);//connect<br>The default is true and the connection is not closed<br>is executed after a command<br>Reqftp.keepalive = false;<br>Specify what commands to execute<br>Reqftp.method = WebRequestMethods.Ftp.AppendFile;<br>Notifies server file size when uploading a file<br>Reqftp.contentlength = fileinf.length;<br>Buffer size set to KB<br>int bufflength = 2048;<br>byte[] buff = new byte[bufflength];<br>int contentlen;<br>Open a file stream (System.IO.FileStream) to read the uploaded file<br>FileStream fs = Fileinf.openread ();<br>Try<br>{<br>StreamReader Dsad = new StreamReader (fs);<br>Fs. Seek (size, seekorigin.begin);<br>Write the uploaded file to the stream<br>Stream STRM = Reqftp.getrequeststream ();<br>KB per read file stream<br>Contentlen = Fs. Read (buff, 0, bufflength);<br>Stream content does not end<br>While (contentlen! = 0)<br>{<br>Write content from file stream to upload stream<br>Strm. Write (buff, 0, contentlen);<br>Contentlen = Fs. Read (buff, 0, bufflength);<br>}<br>Close two streams</p><p><p>Strm. Close ();<br>Fs. Close ();<br>ErrorInfo = "done";<br>Return true;<br>}<br>Catch (Exception Ex)<br>{<br>ErrorInfo = String. Format ("unable to complete upload because {0}", ex. Message);<br>Return false;<br>}<br>}<br>#endregion<br>#region Download files<br>public bool Download (string ftpfilepath, string filePath, string fileName, out string errorinfo)//// The code above implements the ability to download files from an FTP server<br>{<br>Try<br>{<br>FilePath = filepath.replace ("my computer \ \", "");<br>String onlyfilename = Path.getfilename (fileName);<br>String newfilename = FilePath + onlyfilename;<br>If (file.exists (newfilename))<br>{<br>ErrorInfo = String. Format ("local file {0} already exists, could not be downloaded", newfilename);<br>Return false;<br>}<br>Ftpfilepath = Ftpfilepath. Replace ("\ \", "/");<br>String url = "ftp://" + ftpfilepath;<br>Connect (url);//connect</p></p><p>Reqftp.credentials = new NetworkCredential (ftpuserid, ftppassword);<br>FtpWebResponse response = (ftpwebresponse) Reqftp.getresponse ();<br>Stream FtpStream = Response. GetResponseStream ();<br>Long cl = Response. contentlength;<br>int buffersize = 2048;<br>int readcount;<br>byte[] buffer = new byte[buffersize];<br>Readcount = Ftpstream.read (buffer, 0, buffersize);<br>FileStream outputstream = new FileStream (newfilename, filemode.create);<br>While (readcount > 0)<br>{<br>Outputstream.write (buffer, 0, readcount);<br>Readcount = Ftpstream.read (buffer, 0, buffersize);<br>}<br>Ftpstream.close ();<br>Outputstream.close ();<br>Response. Close ();<br>ErrorInfo = "";<br>Return true;<br>}<br>Catch (Exception Ex)<br>{<br>ErrorInfo = String. Format ("cannot Download because {0}", ex.) Message);<br>Return false;<br>}<br>}<br>#endregion<br>#region Delete Files<br>public void Deletefilename (string FileName)<br>{<br>Try<br>{<br>FileInfo fileinf = new FileInfo (fileName);<br>String uri = "ftp://" + ftpserverip + "/" + fileinf.name;<br>Connect (uri);//connect<br>The default is true and the connection is not closed<br>is executed after a command<br>Reqftp.keepalive = false;<br>Specify what commands to execute<br>Reqftp.method = WebRequestMethods.Ftp.DeleteFile;<br>FtpWebResponse response = (ftpwebresponse) Reqftp.getresponse ();<br>Response. Close ();<br>}<br>Catch (Exception Ex)<br>{<br>MessageBox.Show (ex. Message, "delete error");<br>}<br>}<br>#endregion<br>#region Create a directory on FTP<br>public void MakeDir (string DirName)<br>{<br>Try<br>{<br>String uri = "ftp://" + ftpserverip + "/" + dirName;<br>Connect (uri);//connect<br>Reqftp.method = WebRequestMethods.Ftp.MakeDirectory;<br>FtpWebResponse response = (ftpwebresponse) Reqftp.getresponse ();<br>Response. Close ();<br>}<br>Catch (Exception Ex)<br>{<br>MessageBox.Show (ex. Message);<br>}<br>}<br>#endregion<br>#region Delete Directories on FTP<br>public void Deldir (string DirName)<br>{<br>Try<br>{<br>String uri = "ftp://" + ftpserverip + "/" + dirName;<br>Connect (uri);//connect<br>Reqftp.method = WebRequestMethods.Ftp.RemoveDirectory;<br>FtpWebResponse response = (ftpwebresponse) Reqftp.getresponse ();<br>Response. Close ();<br>}<br>Catch (Exception Ex)<br>{<br>}<br>}<br>#endregion<br>#region Get file Size on FTP<br>Public long GetFileSize (string Filename)<br>{<br>Long fileSize = 0;<br>filename = Filename. Replace ("\ \", "/");<br>Try<br>{<br>FileInfo fileinf = new FileInfo (filename);</p><p>String uri1 = "ftp://" + ftpserverip + "/" + fileinf.name;<br>String uri = filename;<br>String uri = "ftp://" + filename;<br>Connect (uri);//connect<br>Reqftp.method = WebRequestMethods.Ftp.GetFileSize;<br>FtpWebResponse response = (ftpwebresponse) Reqftp.getresponse ();<br>FileSize = Response. contentlength;<br>Response. Close ();<br>}<br>Catch (Exception Ex)<br>{<br>MessageBox.Show (ex. Message);<br>}<br>Return fileSize;<br>}<br>#endregion<br>Renaming files on #region FTP<br>public void Rename (string currentfilename, string Newfilename)<br>{<br>Try<br>{<br>FileInfo fileinf = new FileInfo (currentfilename);<br>String uri = "ftp://" + ftpserverip + "/" + fileinf.name;<br>Connect (uri);//connect<br>Reqftp.method = WebRequestMethods.Ftp.Rename;<br>Reqftp.renameto = newfilename;<br>FtpWebResponse response = (ftpwebresponse) Reqftp.getresponse ();<br>Stream FtpStream = Response. GetResponseStream ();<br>Ftpstream.close ();<br>Response. Close ();<br>}<br>Catch (Exception Ex)<br>{<br>MessageBox.Show (ex. Message);<br>}<br>}<br>#endregion<br>#region Get Clear Documentation<br>Public string[] getfilesdetaillist ()<br>{<br>return getfilelist ("ftp://" + ftpserverip + "/", WebRequestMethods.Ftp.ListDirectoryDetails);<br>}<br>Public string[] getfilesdetaillist (string Path)<br>{<br>Path = Path. Replace ("\ \", "/");<br>return getfilelist ("ftp://" + path, WebRequestMethods.Ftp.ListDirectoryDetails);<br>}<br>#endregion<br>}<br>}</p><p><p>FTP Upload Class</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.