C # implement FTP upload,

Source: Internet
Author: User
Tags ftp file

C # implement FTP upload,

1 /// <summary> 2 /// Upload File 3 /// </summary> 4 /// <param name = "fileinfo"> file to be uploaded </param> 5 // <param name = "targetDir"> target path </param> 6 /// <param name = "hostname"> ftp address </param> 7 // /<param name = "username"> ftp username </param> 8 // <param name = "password"> ftp password </param> 9 public static void UploadFile (FileInfo fileinfo, string targetDir, string hostname, string username, string password) 10 {11 // 1. check Target 12 string target; 13 if (targetDir. trim () = "") 14 {15 return; 16} 17 target = Guid. newGuid (). toString (); // use the temporary file name 18 19 20 string URI = "FTP: //" + hostname + "/" + targetDir + "/" + target; 21 // WebClient webcl = new WebClient (); 22 System. net. ftpWebRequest ftp = GetRequest (URI, username, password); 23 24 // set the FTP command to be executed, 25 // ftp. method = System. net. webRequestMethods. ftp. listD IrectoryDetails; // assume that the file list in the specified path is displayed. 26 ftp. method = System. net. webRequestMethods. ftp. uploadFile; 27 // specifies the data type of file transmission 28 ftp. useBinary = true; 29 ftp. usePassive = true; 30 31 // tell the ftp file size 32 ftp. contentLength = fileinfo. length; 33 // set the buffer size to 2KB 34 const int BufferSize = 2048; 35 byte [] content = new byte [BufferSize-1 + 1]; 36 int dataRead; 37 38 // open a file stream (System. IO. fileStream) to read the uploaded file 39 using (FileS Tream fs = fileinfo. openRead () 40 {41 try 42 {43 // write the uploaded file to the Stream 44 using (Stream rs = ftp. getRequestStream () 45 {46 do 47 {48 // 2KB 49 dataRead = fs for each file stream read. read (content, 0, BufferSize); 50 rs. write (content, 0, dataRead); 51} while (! (DataRead <BufferSize); 52 rs. close (); 53} 54 55} 56 catch (Exception ex) {} 57 finally 58 {59 fs. close (); 60} 61 62} 63 64 ftp = null; 65 // set FTP command 66 ftp = GetRequest (URI, username, password); 67 ftp. method = System. net. webRequestMethods. ftp. rename; // renamed 68 ftp. renameTo = fileinfo. name; 69 try 70 {71 ftp. getResponse (); 72} 73 catch (Exception ex) 74 {75 ftp = GetRequest (URI, username, password); 76 ftp. method = System. net. webRequestMethods. ftp. deleteFile; // Delete 77 ftp. getResponse (); 78 throw ex; 79} 80 finally 81 {82 // fileinfo. delete (); 83} 84 85 // you can record a log "Upload" + fileinfo. fullName + "upload to" + "FTP: //" + hostname + "/" + targetDir + "/" + fileinfo. name + "successful. "); 86 ftp = null; 87 88 # region 89/***** 90 * FtpWebResponse 91 *****/92 // FtpWebResponse ftpWebResponse = (FtpWebResponse) ftp. getResponse (); 93 # endregion 94} 95 96 private static FtpWebRequest GetRequest (string URI, string username, string password) 97 {98 // create a Class Object 99 FtpWebRequest result = (FtpWebRequest) FtpWebRequest Based on the server information. create (URI); 100 // provide the authentication information 101 result. credentials = new System. net. networkCredential (username, password); 102 // sets whether to maintain the control connection to the FTP server after the request is complete. The default value is true103 result. keepAlive = false; 104 return result; 105}

 

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.