FTP upload Download C #

Source: Internet
Author: User

 Public classMyftpclass {Private ReadOnly string_destip; Private ReadOnly string_username; Private ReadOnly string_password;  PublicMyftpclass (stringDesip,stringUserName,stringpassowrd) {             This. _destip =Desip;  This. _username =UserName;  This. _password =PASSOWRD; }        PrivateFtpWebRequest openrequest (Uri Uri,stringFtpmethod) {FtpWebRequest Request= WebRequest.Create (URI) asFtpWebRequest; if(Request! =NULL) {request. Credentials=NewNetworkCredential (_username, _password); Request. Usebinary=true; Request. Method=Ftpmethod; returnrequest; }            Else            {                return NULL; }                    }        PrivateFtpWebResponse openresponse (Uri Uri,stringFtpmethod) {FtpWebRequest Request= This.            OpenRequest (URI, Ftpmethod); if(Request! =NULL) {ftpwebresponse response= Request. GetResponse () asFtpWebResponse; returnresponse; }            Else            {                return NULL; }                                }        /// <summary>        ///local file path to upload/// </summary>        /// <param name= "Uploadfilepath" >shaped like: d:\\folder1\\2.jpg</param>        /// <param name= "Desname" >shape: 123.jpg or Subfolder1/123.jpg</param>        /// <returns></returns>         Public BOOLUpload (stringUploadfilepath,stringdesname) {Stream Requeststream=NULL; FtpWebResponse Response=NULL; Try{URI Uri=NewUri (string. Format ("Ftp://{0}/{1}", _destip, Desname)); FtpWebRequest Request=openrequest (URI, WebRequestMethods.Ftp.UploadFile); Requeststream=request.                GetRequestStream (); FileStream FS=File.Open (Uploadfilepath, FileMode.Open); byte[] Buff =New byte[1024x768]; intBuffread =0;  while(true)                {                    intn =FS. Read (Buff, buffread, buff.                    Length); if(n = =0)                    {                         Break; }                    //writes a file stream to the request streamrequestStream.Write (Buff,0, N); } Response=(ftpwebresponse) request.                GetResponse (); return true; }            Catch(Exception) {Throw NewException ("Upload file failed! "); }            finally{requeststream.close (); Response.            Close (); }                    }        /// <summary>        ///Download File/// </summary>        /// <param name= "Serverfilename" >the name of the file to download. Shape: 123.jpg or Subfolder1/123.jpg</param>        /// <param name= "LocalPath" >the path to the local download. Shaped like: d:\\a\\b\\</param>        /// <param name= "LocalName" >the name that is saved locally. Shape: 3.jpg</param>        /// <returns></returns>         Public BOOLDownload (stringServerfilename,stringLocalPath,stringlocalname) {Stream Responsestream=NULL; FileStream FileStream=NULL; Try{URI Uri=NewUri (string. Format ("Ftp://{0}/{1}", _destip, Serverfilename)); FtpWebResponse Downloadresponse=openresponse (URI, WebRequestMethods.Ftp.DownloadFile); Responsestream=Downloadresponse.getresponsestream (); FileStream= File.create (LocalPath +localname); byte[] buffer =New byte[1024x768];  while(true)                {                    varBytesread = responsestream.read (buffer,0, buffer.                    Length); if(Bytesread = =0)                         Break; //write a response stream to a fileFileStream.Write (Buffer,0, Bytesread); }                                return true; }            Catch            {                Throw NewException ("failed to get download file! "); }            finally{filestream.close ();            Responsestream.close (); }        }    }
Note the read-out and write issues for stream files :
For example
int n =  FS. Read (Buff, buffread, buff. Length);
Bufferread and Buff.length refer to the index and length of the array buff.
Stream file FS automatically maintains a pointer position that automatically moves back n bytes whenever read n bytes.

FileStream.Write (buffer, 0, bytesread); 

FTP upload Download C #

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.