FTP File Upload

Source: Internet
Author: User
Tags ftp file

FTP File Upload

FTP File Download

Delete an FTP File

Create directory for FTP File Operations

 

Uploading files is a common function. A module for uploading images was created some time ago. I started using the shared folder method, and later found that this method is not very good. Therefore, the system was decisively killed and later used FTP for upload. I personally feel that the FTP method is still quite useful, so I 'd like to share with you.


Upload CoreCode:

 

         ///   <Summary>         ///  Upload files through FTP  ///   </Summary>          ///   <Param name = "fileupload">  Upload Control  </Param>          ///   <Param name = "ftpserverip">  File Upload Server IP Address  </Param>          ///   <Param name = "ftpuserid">  Server Username </Param>          ///   <Param name = "ftppassword">  Server password  </Param>          ///   <Returns> </returns>          Public    String Upload (fileupload, String Ftpserverip, String Ftpuserid, String  Ftppassword ){  String Filename = Fileupload. filename;  String Sret = "  Uploaded successfully!  "  ; Fileinfo fileinf = New  Fileinfo (fileupload. postedfile. filename );  String Uri = "  FTP ://  " + Ftpserverip + "  / " + Filename; ftpwebrequest reqftp;  //  Create an ftpwebrequest object based on Uri Reqftp = (ftpwebrequest) ftpwebrequest. Create ( New  Uri (URI ));  //  FTP user name and password Reqftp. Credentials = New  Networkcredential (ftpuserid, ftppassword );  //  The default value is true, and the connection will not be closed. //  Executed after a command Reqftp. keepalive = False  ;  //  Specify the command to be executed Reqftp. method = Webrequestmethods. FTP. uploadfile;  //  Data transmission type Reqftp. usebinary = True  ; Reqftp. usepassive = False  ; //  Notify the server of file size when uploading files Reqftp. contentlength = Fileinf. length;  //  Set the buffer size to 2 kb.              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  {  //  Write uploaded files to the stream Stream STRM = Reqftp. getrequeststream ();  //  2 kb of each file stream read Contentlen = FS. Read (buff, 0  , Bufflength ); //  The stream content has not ended                  While (Contentlen! = 0  ){  //  Write content from file stream to upload stream STRM. Write (buff, 0  , Contentlen); contentlen = FS. Read (buff, 0  , Bufflength );}  //  Close two streams STRM. Close (); FS. Close ();}  Catch  (Exception ex) {sret = Ex. Message ;}  Return  Sret ;} 

 


The above is just a simple upload. Of course, you still need to verify the file before uploading, such as the file format or file size. For simple verification, seeDetermine the file format and size before uploading images.

 

To prevent duplicate names, you can use guid to generate a random sequence. Ideally, no computer or computer cluster generates two identical guids. Of course, the probability of repetition is not 0, but it is very small.

 

/// <Summary>///Generate globally unique identifier/// </Summary>/// <Returns> </returns>Public StringStrguid (){StringStrguid =Guid. newguid (). tostring ();ReturnStrguid ;}

 

 

Call this method and splice the returned sequence with the file name to effectively avoid file duplication.Of course, you can also splice the file name with the current system time, which may make you feel safer. The specific method is wise.



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.