FTP File Upload

Source: Internet
Author: User
Tags ftp file
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. Core uploaded code: [CSHARP] /// <summary> /// FTP uploads a file /// </Summary> /// <Param name = "fileupload"> upload Control </param>/ // <Param name = "ftpserverip"> File Upload Server IP address </param> /// <Param name = "ftpuserid"> Server user name </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 = "Upload successful! "; Fileinfo fileinf = new fileinfo (fileupload. postedfile. filename); string uri = "ftp: //" + ftpserverip + "/" + filename; ftpwebrequest reqftp; // create ftpwebrequest object reqftp = (ftpwebrequest) ftpwebrequest according to the URI. 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 // reqftp is executed after a command. keepalive = false; // specifies the command to execute reqftp. method = web Requestmethods. FTP. uploadfile; // specify the data transmission type reqftp. usebinary = true; reqftp. usepassive = false; // notifies the server of file size reqftp when uploading files. 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 the uploaded file to the stream STRM = reqftp. getrequeststream (); // 2kb conten of each file stream read Tlen = FS. Read (buff, 0, bufflength); // The stream content does not end 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 ;} /// <summary> /// FTP uploads a file /// </Summary> /// <Param name = "fileupload"> upload Control </param> /// <param name = "ftpserverip"> File Upload Server IP </param> // <Param name = "ftpuserid"> Server user name </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 = "Upload successful! "; Fileinfo fileinf = new fileinfo (fileupload. postedfile. filename); string uri = "ftp: //" + ftpserverip + "/" + filename; ftpwebrequest reqftp; // create ftpwebrequest object reqftp = (ftpwebrequest) ftpwebrequest according to the URI. 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 // reqftp is executed after a command. keepalive = false; // specifies the command to execute reqftp. method = web Requestmethods. FTP. uploadfile; // specify the data transmission type reqftp. usebinary = true; reqftp. usepassive = false; // notifies the server of file size reqftp when uploading files. 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 the uploaded file to the stream STRM = reqftp. getrequeststream (); // 2kb conten of each file stream read Tlen = FS. Read (buff, 0, bufflength); // The stream content does not end 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 need to verify the file before uploading, such as the file format or file size. For simple verification, see determine the file format and size before uploading an image. 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. [CSHARP] /// <summary> /// generate a globally unique identifier /// </Summary> /// <returns> </returns> Public String strguid () {string strguid = guid. newguid (). tostring (); Return strguid ;} /// <summary> /// generate a globally unique identifier /// </Summary> /// <returns> </returns> Public String strguid () {string strguid = guid. newguid (). tostring (); Return strguid;} calls this method, and Concatenates the returned sequence with the file name to effectively avoid file name 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.

 

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.