none
C# Ftp Upload RRS feed

  • Domanda

  • Hi EveryBody.

    I have this code.. it's work fine on ftp like altervista, but it doesn't work on my company ftp... and it's a problem... Do you have an idea??

    Please note:
    1 i'm trying to pass an uri file for upload like ftp://myftp.server.com/path1/path2/path3/myfile.bin
    2 my company ftp works fine with filezilla and other ftp client

    here is my code:

    private Boolean FtpUpload(JobData myJobData)
            {
                bool RetVal = false;
                try
                {
                    FtpWebRequest myReq = (FtpWebRequest)WebRequest.Create(myJobData.uriFile);
                    myReq.Credentials = new NetworkCredential(myJobData.userName, myJobData.password);
                    myReq.KeepAlive = true;
                    myReq.Method = WebRequestMethods.Ftp.UploadFile;
                    FtpWebResponse res = (FtpWebResponse)myReq.GetResponse();
                    Stream myStream = myReq.GetRequestStream();
                    FileStream fs = null;
                    StreamReader sr = null;
                    if (myJobData.isText == false)
                    {
                        fs = File.OpenRead(myJobData.localFileName);
                        byte[] buffer = new byte[1];
                        int bytesRead = 1;
                        while (bytesRead != 0)
                        {
                            bytesRead = fs.Read(buffer, 0, 1);
                            myStream.Write(buffer, 0, 1);
                        }
                        fs.Close();
                        fs.Dispose();
                    }
                    else
                    {
                        sr = new StreamReader(myJobData.localFileName);
                        byte[] fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd());
                        myStream.Write(fileContents, 0, fileContents.Length);
                        sr.Close();
                        sr.Dispose();
                    }
    
                    myStream.Close();
                    myStream.Dispose();
                    myReq.Abort();
                    GC.Collect();
                    RetVal = true;
                }
                catch (Exception ex)
                {
                    errorLog += DateTime.Now.ToShortDateString() + " " +
                       DateTime.Now.ToShortTimeString() + " --> " +
                       myJobData.uriFile + " --> " +
                       "Exception: " + ex.Message +
                       " Procedura di upload non eseguita!\r\n" +
                       ex.ToString();
                }   
                
    
                return RetVal;
            }

    sabato 24 ottobre 2009 00:17

Risposte

Tutte le risposte