im trying to upload jpeg to ftp server using C#. the file is uploaded but it is corrupted while opening it. this is my code:
/// <summary>
/// Upload HttpPostedFileBase to ftp server
/// </summary>
/// <param name="image">type of HttpPostedFileBase</param>
/// <param name="targetpath">folders path in ftp server</param>
/// <param name="source">jpeg image name</param>
public static void UploadFileToFTP(HttpPostedFileBase image,string targetpath,string source)
{
string ftpurl = ConfigurationManager.AppSettings["Ftp_Images_Domain"];
string ftpusername = ConfigurationManager.AppSettings["Ftp_Images_Usr"];
string ftppassword = ConfigurationManager.AppSettings["Ftp_Images_Pwd"];
try
{
SetMethodRequiresCWD();
string filename = Path.GetFileName(source);
string ftpfullpath = ftpurl + targetpath + source;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = false;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
HttpPostedFileBase myFile = image;
int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(myData, 0, nFileLen);
ftpstream.Close();
ftpstream.Flush();
}
catch (WebException e)
{
String status = ((FtpWebResponse)e.Response).StatusDescription;
}
}
i assume the problem is in the byte array i write to the ftp stream. just cant figure how to fix it. any help is appreciated :-) thank you