FTP file operation upload file

Source: Internet
Author: User
Tags ftp file

Uploading files is a relatively common function, the previous period of time to do a upload image module. Started with the way you shared folders, and later found this method not very good. So decisively kill it, and then choose to use the FTP way to upload. Personally feel the FTP way is still more useful, so share with you.

/// <summary>        ///ftp upload files/// </summary>        /// <param name= "FileUpload" >Uploading Controls</param>        /// <param name= "Ftpserverip" >upload file server IP</param>        /// <param name= "Ftpuserid" >Server user name</param>        /// <param name= "FTPPassword" >Server Password</param>        /// <returns></returns>         Public  stringUpload (FileUpload FileUpload,stringFtpserverip,stringFtpuserid,stringFTPPassword) {            stringfilename =Fileupload.filename; stringSret ="Upload Success! "; FileInfo Fileinf=NewFileInfo (fileUpload.PostedFile.FileName); stringURI ="ftp://"+ Ftpserverip +"/"+filename;            FtpWebRequest reqftp; //to create a FtpWebRequest object from a URIReqftp = (ftpwebrequest) ftpwebrequest.create (NewUri (URI)); //FTP user name and passwordReqftp.credentials =NewNetworkCredential (Ftpuserid, FTPPassword); //The default is true and the connection is not closed//is executed after a commandReqftp.keepalive =false; //specify what commands to executeReqftp.method =WebRequestMethods.Ftp.UploadFile; //specifying data Transfer TypesReqftp.usebinary =true; Reqftp.usepassive=false; //notifies server file size when uploading a fileReqftp.contentlength =fileinf.length; //buffer size set to 2KB            intBuffLength =2048; byte[] Buff =New byte[BuffLength]; intContentlen; //open a file stream (System.IO.FileStream) to read the uploaded fileFileStream fs =Fileinf.openread (); Try            {                //write the uploaded file to the streamStream STRM =Reqftp.getrequeststream (); //2kb per read file streamContentlen = fs. Read (Buff,0, bufflength); //stream content does not end                 while(Contentlen! =0)                {                    //write content from file stream to upload streamStrm. Write (Buff,0, Contentlen); Contentlen= fs. Read (Buff,0, bufflength); }                //Close two streamsSTRM.                Close (); Fs.            Close (); }            Catch(Exception ex) {Sret=Ex.            Message; }            returnSret; }

The above is just a simple upload, of course, before uploading also need to do some validation of the file, such as file format or file size. For simple verification, please refer to the upload image before uploading to determine the file format and size.

To prevent duplicate names, you can use the global unique identifier GUID to generate a random sequence that, ideally, does not generate two identical GUIDs for any computer or computer cluster. Of course, the odds of repetition are not 0, but they are very small.

/// <summary> /// generate globally unique identifiers /// </summary> /// <returns></returns>  Public string strGUID () {    string strguid = guid.newguid (). ToString ();     return strGUID;}

Calling this method and then stitching the returned sequence with the file name can effectively avoid the case of duplicate file names. Of course, you can use the system's current time to join the file name, so maybe you feel more insured. Specific use of that method is a matter of opinion.

FTP file operation upload file

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.