0

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

Basilf
  • 401
  • 5
  • 17

2 Answers2

3

byte[] myData is never initialized with the image data. Below is the correct code. I have removed some unwanted lines from the code. Try it and let me know if it worked fine or not

/// <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 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;

        using ( Stream ftpstream = ftp.GetRequestStream() )
            image.InputStream.CopyTo(ftpstream)
    } catch (WebException e) {
        String status = ((FtpWebResponse)e.Response).StatusDescription;
    }
}
Vikhram
  • 4,294
  • 1
  • 20
  • 32
  • I came into this today, and when I read about CopyTo method I thought _"of course..."_ thanks. You can also avoid zero length files by adding `image.Seek(0, SeekOrigin.Begin);` before `using ...` – Alexandre Sep 21 '16 at 01:15
0

I didn't try it with HttpPostedFileBase but the below method works and should give you an idea about whats happening in your code.

public class Test
{
    public static void UploadFileToFTP()
    {

        string ftpurl = "ftp://ServerName:21/test.txt";

        try
        {
            string filename = "F:\\testing.txt";
            string ftpfullpath = ftpurl;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential();

            //ftp.KeepAlive = false;
            ftp.UsePassive = false;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;
            byte[] myFile = File.ReadAllBytes(filename);
            ftp.ContentLength = myFile.Length;
            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(myFile, 0, myFile.Length);
            ftpstream.Close();
            ftpstream.Flush();
        }
        catch (WebException e)
        {
            String status = ((FtpWebResponse)e.Response).StatusDescription;
        }
    }
}
loneshark99
  • 706
  • 5
  • 16