protected void Button1_Click(object sender, EventArgs e)
{
string server = "-------"; //your ip address
string ftpPath = "ftp://" + server + "//Files//" +FileUpload1.FileName;
string uname = "-----";
string password = "----------";
string filePath = Server.MapPath("~") + "\\" + FileUpload1.FileName;
try
{
UploadToFTP(ftpPath,filePath,uname,password);
}
catch (Exception ex)
{
//logic here
}
}
`private bool UploadToFTP (строка strFTPFilePath, строка strLocalFilePath, строка strUserName, строка strPassword) {try {// Создать объект запроса FTP и указать полный путь // System.Net.WebRequest.Create (strFTPFilePath);FtpWebRequest reqObj = (FtpWebRequest) WebRequest.Create (strFTPFilePath);
//Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
//If you want to access Resourse Protected,give UserName and PWD
reqObj.Credentials = new NetworkCredential(strUserName, strPassword);
// Copy the contents of the file to the byte array.
byte[] fileContents = File.ReadAllBytes(strLocalFilePath);
reqObj.ContentLength = fileContents.Length;
//Upload File to FTPServer
Stream requestStream = reqObj.GetRequestStream();
// Stream requestStream = response.GetResponseStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
response.Close();
Label1.Text = "File Transfered Completed" + response.StatusDescription;
}
catch (Exception Ex)
{
throw Ex;
}
return true;
} `