Hi nauna,
Check with below code.
C#
protected string UploadFolderPath = "http://mycherry2.msonlinegroup.com/DynamicImage/";
protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
UploadFolderPath = UploadImages(e);
}
public string UploadImages(AjaxFileUploadEventArgs e)
{
string uploadFileName = "";
if (!string.IsNullOrEmpty(e.FileName))
{
ext = Path.GetExtension(e.FileName).ToLower();
if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
{
uploadFileName = System.IO.Path.GetFileName(e.FileName);
}
string ftp = ftppath;
string ftpFolder = domainname + "/DynamicImage/" + uploadFileName;
byte[] fileBytes = null;
string fileName = Path.GetFileName(uploadFileName);
using (Stream fs = e.GetStreamContents())
{
using (BinaryReader br = new BinaryReader(fs))
{
fileBytes = br.ReadBytes((Int32)fs.Length);
}
}
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
path = returnpath + "/DynamicImage/" + uploadFileName;
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
HttpContext.Current.Session["Path"] = path;
return path;
}