There are two main classes you can use to gain access to a remote Ftp Server in your ASP.NET web application, FtpWebResponse and FtpWebRequest. Here is a sample code that uses a FileUpload control to upload a file to the remote server:
FtpWebRequest wReq = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myftpsrver.com/" + FileUpload1.FileName));
wReq.UseBinary = true;
wReq.Method = WebRequestMethods.Ftp.UploadFile;
wReq.Credentials = new NetworkCredential("admin", "mypass");
FileInfo fi = new FileInfo(FileUpload1.PostedFile.FileName);
byte[] bytes = new byte[fi.Length];
FileStream fs = fi.OpenRead();
fs.Read(bytes, 0,(int) fi.Length);
fs.Close();
Stream rs = wReq.GetRequestStream();
rs.Write(bytes, 0, bytes.Length);
rs.Close();
FtpWebResponse wResp = (FtpWebResponse)wReq.GetResponse();
Label1.Text = "Code: " + wResp.StatusCode.ToString() + ", Description: " + wResp.StatusDescription;
No comments:
Post a Comment