ASP. NET uploads files via FTP

Source: Internet
Author: User
Tags create directory

 Public byte[] Ftpdownload (string filePath) {try {ftpwebrequest reqftp = Get                FtpWebRequest (New Uri (fptserver + Ftpmappath + filePath), WebRequestMethods.Ftp.DownloadFile);                FtpWebResponse response = (ftpwebresponse) reqftp.getresponse ();                if (response = = NULL) {return null; } Stream Retstream = response.                GetResponseStream ();                list<byte> Contentarray = new list<byte> ();                int bt = Retstream.readbyte ();                    while (BT >-1) {Contentarray.add ((byte) bt);                BT = Retstream.readbyte ();                } retstream.dispose ();            return Contentarray.toarray (); } catch (Exception ex) {Loghelper.logerror ("Ftpdownload failed!                ", ex);            return null; }} PrivaTe ftpwebrequest getftpwebrequest (Uri Uri, string method) {FtpWebRequest ftpclientrequest = (ftpwebrequ            EST) webrequest.create (URI);            Ftpclientrequest.proxy = null;            Ftpclientrequest.credentials = new NetworkCredential (Ftpuserid, FTPPassword);            Ftpclientrequest.usebinary = true;            Ftpclientrequest.usepassive = true;            Ftpclientrequest.method = Method;            Ftpclientrequest.timeout =-1;        return ftpclientrequest;                } private bool Ftpuploadfile (byte[] filecontent,string fullPath) {try {                String ftpfiledir = Path.getdirectoryname (FullPath);                This always executes Ftpmakedirbypath (ftpfiledir.replace ("\ \", "/"));                FtpWebRequest reqftp = getftpwebrequest (new Uri (Fptserver+fullpath), WebRequestMethods.Ftp.UploadFile);                Stream stream = Reqftp.getrequeststream (); Stream. Write (filecontent, 0, FileconTent.                Length); Stream.                Flush (); Stream.                Close ();            return true; } catch (Exception ex) {Loghelper.logerror ("Ftpuploadfile failed!                ", ex);            return false; }}////<summary>//Create a multilevel directory based on the path///</summary>//<param N Ame= "Dirpath" ></param>///<returns></returns> private bool Ftpmakedirbypath (String dir Path) {if (string.            IsNullOrEmpty (Dirpath)) {return false;            } dirpath=dirpath.replace ("\ \", "/");            String[] dirs= dirpath.split (new char[] {'/'}, Stringsplitoptions.removeemptyentries); if (dirs! = null && dirs.                Length > 0) {bool result = false;                String des = fptserver; try {for (int i = 0; i < dirs. LengTh                        i++) {result = Ftpmakedir (DES, dirs[i]);                    des = des + "/" + dirs[i];                } return true; } catch (Exception ex) {Loghelper.logerror ("Ftpmakedirbypath failed!                    ", ex);                return false;        }} return false; }//Create directory private bool Ftpmakedir (string destinationpath,string dirName) {ftpwebresponse R            Esponse = null;                try {uri uri = new Uri (destinationpath+ "/" + dirName);                FtpWebRequest reqftp = Getftpwebrequest (URI, WebRequestMethods.Ftp.MakeDirectory);                Response = (FtpWebResponse) reqftp.getresponse (); Response.                Close ();            return true; } catch (Exception ex) {Loghelper.logerror ("Ftpmakedir failed! ", Ex);            return false;         }        }

ASP. NET uploads files via FTP

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.