FTP

Last post 05-01-2008 10:49 by Spectra. 53 replies.
Page 4 of 4 (54 items) < Previous 1 2 3 4
Sort Posts: Previous Next
  • 12-27-2006 14:58 In reply to

    Re: FTP

    With the help of various other posts I've put together C# source code that works. Hopefully this will help others out there.



    public static bool UploadFile(string host, string directory, string filePathName, ICredentials credentials) {
    Uri uri = null;

    string fileName = Path.GetFileName(filePathName);
    if (directory == null)
    uri = new Uri(string.Format(@"ftp://{0}/", host));
    else
    uri = new Uri(string.Format(@"ftp://{0}/{1}/", host, directory));

    try {

    FtpRequestCreator creator = new FtpRequestCreator();
    WebRequest.RegisterPrefix("ftp:", creator);
    FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uri);
    ftpWebRequest.Credentials = credentials;

    // Getting the Request stream
    Stream ftpRequestStream = ftpWebRequest.GetRequestStream();

    StreamReader responseReader = new StreamReader(ftpRequestStream);

    // Just ignore the result, but read it.
    String responseString = responseReader.ReadToEnd();


    // Open the input file. If the file does not exist, it's an error.
    FileStream filestream = new FileStream(filePathName, FileMode.Open);

    // Create the reader for the local file data.
    BinaryReader fileReader = new BinaryReader(filestream);


    // Opening the data connection, this must be done before we issue the command.
    Stream ftpResponseStream = ftpWebRequest.GetResponse().GetResponseStream();
    BinaryWriter dataWriter = new BinaryWriter(ftpResponseStream);

    // Prepare to send commands to the server.
    StreamWriter commandWriter = new StreamWriter(ftpRequestStream);

    // Set transfer type to IMAGE (binary).
    commandWriter.Write("TYPE I\r\n");
    commandWriter.Flush();

    // Reading the request output
    responseReader = new StreamReader(ftpRequestStream);
    responseString = responseReader.ReadToEnd();

    // Write the command to the request stream.
    String cmd = "stor " + fileName + "\r\n";
    commandWriter.Write(cmd);
    commandWriter.Flush(); // We MUST flush before we start reading from both response and request

    // Reading the request output
    responseString = responseReader.ReadToEnd();

    // Allocate buffer for the data, which will be written in blocks.
    int bufsize = 1024;
    byte[] buf = new byte[bufsize];
    int xcount;

    while ((xcount = fileReader.Read(buf, 0, bufsize)) > 0) {
    // Send next buffer over the data connection.
    dataWriter.Write(buf, 0, xcount);
    }

    fileReader.Close();
    filestream.Close();

    dataWriter.Close();

    responseReader.Close();


    return true;

    } catch (Exception ex) {
    throw ex;
    }
    }
  • 12-27-2006 15:02 In reply to

    Re: FTP

    vault.opennetcf.com
    Username: guest
    Password: guest

    quote:
    Originally posted by jodybreaux

    I am trying to get the Ftp functionality to work, i have the source code, but have been unsuccessful in getting into the 'Vault' to get the ftp example...

    Does anyone have the correct address/login for the vault, or easier, the ftp example that they can send? Please advise. Thanks in advance.


  • 06-11-2007 1:26 In reply to

    Re: FTP

    Hi,

    When using the upload code as found in the vault, I am also finding that the file uploaded has 0 bytes.

    Has there been any fix for this code so that both download and upload work perfectly with the opennetcf.net.ftp class.

    Regards

    Rajesh.

    quote:
    Originally posted by kenh

    I have problem using the FTP class from from OpenNETCT.NET.ftp to upload file. I can connect successfully to the ftp and able to upload file to the server. However the file upload has zero bytes. No data was send.
    Any ideas? I used the sample from the vault (FTPFile)

    Hi illancolombick,
    Are you using local address for your FTP server and connect through activesync using cradle? The FTP class use Dns to resolve the hostname, so you might get socket exception or Host unavailable error.
    I spent hours on this and only realize the cause.



  • 09-17-2007 23:31 In reply to

    Re: FTP

    Hi, Its giving Argument Exception , Iam using VS 2005 and CF 1.0 for my code. Is any other Cab file to be install on PDA. and morw on Icredentail like username and password passing for FTP.

    Help me




    public static bool UploadFile(string host, string directory, string filePathName, ICredentials credentials) {
    Uri uri = null;

    string fileName = Path.GetFileName(filePathName);
    if (directory == null)
    uri = new Uri(string.Format(@"ftp://{0}/", host));
    else
    uri = new Uri(string.Format(@"ftp://{0}/{1}/", host, directory));

    try {

    FtpRequestCreator creator = new FtpRequestCreator();
    WebRequest.RegisterPrefix("ftp:", creator);
    FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uri);
    ftpWebRequest.Credentials = credentials;

    // Getting the Request stream
    Stream ftpRequestStream = ftpWebRequest.GetRequestStream();

    StreamReader responseReader = new StreamReader(ftpRequestStream);

    // Just ignore the result, but read it.
    String responseString = responseReader.ReadToEnd();


    // Open the input file. If the file does not exist, it's an error.
    FileStream filestream = new FileStream(filePathName, FileMode.Open);

    // Create the reader for the local file data.
    BinaryReader fileReader = new BinaryReader(filestream);


    // Opening the data connection, this must be done before we issue the command.
    Stream ftpResponseStream = ftpWebRequest.GetResponse().GetResponseStream();
    BinaryWriter dataWriter = new BinaryWriter(ftpResponseStream);

    // Prepare to send commands to the server.
    StreamWriter commandWriter = new StreamWriter(ftpRequestStream);

    // Set transfer type to IMAGE (binary).
    commandWriter.Write("TYPE I\r\n");
    commandWriter.Flush();

    // Reading the request output
    responseReader = new StreamReader(ftpRequestStream);
    responseString = responseReader.ReadToEnd();

    // Write the command to the request stream.
    String cmd = "stor " + fileName + "\r\n";
    commandWriter.Write(cmd);
    commandWriter.Flush(); // We MUST flush before we start reading from both response and request

    // Reading the request output
    responseString = responseReader.ReadToEnd();

    // Allocate buffer for the data, which will be written in blocks.
    int bufsize = 1024;
    byte[] buf = new byte[bufsize];
    int xcount;

    while ((xcount = fileReader.Read(buf, 0, bufsize)) > 0) {
    // Send next buffer over the data connection.
    dataWriter.Write(buf, 0, xcount);
    }

    fileReader.Close();
    filestream.Close();

    dataWriter.Close();

    responseReader.Close();


    return true;

    } catch (Exception ex) {
    throw ex;
    }
    }

  • 10-05-2007 5:13 In reply to

    Re: FTP

    quote:
    Originally posted by msmrajesh

    Hi,

    When using the upload code as found in the vault, I am also finding that the file uploaded has 0 bytes.

    Has there been any fix for this code so that both download and upload work perfectly with the opennetcf.net.ftp class.

    Regards

    Rajesh.

    quote:
    Originally posted by kenh

    I have problem using the FTP class from from OpenNETCT.NET.ftp to upload file. I can connect successfully to the ftp and able to upload file to the server. However the file upload has zero bytes. No data was send.
    Any ideas? I used the sample from the vault (FTPFile)

    Hi illancolombick,
    Are you using local address for your FTP server and connect through activesync using cradle? The FTP class use Dns to resolve the hostname, so you might get socket exception or Host unavailable error.
    I spent hours on this and only realize the cause.






    Hi,
    I've the same problem => Downloaded file has zero bytes
    because xcount = 0 when xcount = fileReader.Read(buf, 0, bufsize)

    Has anyone solved this problem?
  • 10-15-2007 5:27 In reply to

    Re: FTP

    As a VB person i had some problems getting all the FTP code from C# to VB.. I hope this code helps the next person...thanks to all the C# people that made this happen.


    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Net
    Imports System.IO

    Imports OpenNETCF.Net
    Imports OpenNETCF.Net.Ftp

    Public Class FTPCode

    '' FTP Server
    Dim sFTPServer As String = "ftp://ftp.yourftpserver.com"
    '' FTP User Account
    Dim sUserName As String = "UserName"
    ''FTP sPassword
    Dim sPassword As String = "Password"


    Public Sub UploadFile(ByVal sFilePath As String)

    Dim FtpRequestCreator As New FtpRequestCreator
    WebRequest.RegisterPrefix("ftp:", FtpRequestCreator)

    Dim oFtpWebRequest As New FtpWebRequest(New Uri(sFTPServer))
    oFtpWebRequest.Credentials = New NetworkCredential(sUserName, sPassword)

    ' Getting the Request stream
    Dim oFtpRequestStream As Stream = oFtpWebRequest.GetRequestStream()

    Dim oStreamReader As New StreamReader(oFtpRequestStream)

    'Just ignore the result, but read it.
    Dim oReader As String = oStreamReader.ReadToEnd

    'Open the input file. If the file does not exist, it's an error.
    Dim oFileStream As New FileStream(sFilePath, FileMode.Open)

    'Create the reader for the local file data.
    Dim oFileReader As BinaryReader = New BinaryReader(oFileStream)

    'Opening the data connection, this must be done before we issue the command.
    Dim oFtpResponseStream As Stream = oFtpWebRequest.GetResponse().GetResponseStream()
    Dim oDataWriter As New BinaryWriter(oFtpResponseStream)



    'Prepare to send commands to the server.
    Dim cmdWriter As New StreamWriter(oFtpRequestStream)

    'Set transfer type to IMAGE (binary).
    cmdWriter.Write("TYPE I" & vbCrLf)
    cmdWriter.Flush()

    'Reading the request output
    Dim oStreamReader2 As New StreamReader(oFtpRequestStream)
    oReader = oStreamReader.ReadToEnd()

    'Reading the request output
    Dim sReader As String
    sReader = oStreamReader2.ReadToEnd()

    'Write the command to the request stream.
    cmdWriter.Write("STOR " + Path.GetFileName(sFilePath) + vbCrLf)
    cmdWriter.Flush()

    sReader = oStreamReader2.ReadToEnd()


    'Allocate buffer for the data, which will be written in blocks.
    Dim bytes As Integer
    Dim bufsize As Integer = 1024
    Dim buffer(bufsize) As Byte

    While True
    bytes = oFileReader.Read(buffer, 0, bufsize)
    oDataWriter.Write(buffer, 0, bytes)
    If bytes <= 0 Then
    Exit While
    End If
    End While

    oFileReader.Close()
    oFileStream.Close()
    oDataWriter.Close()
    oStreamReader.Close()

    End Sub

    End Class




    Thanks,
    Neal
  • 12-12-2007 4:58 In reply to

    Re: FTP

    Hello,

    This works great when I'am connected via Wlan but if I try it via GPRS I always got the message could not login onto the FTP server?

    How can this be solved?
  • 01-16-2008 11:41 In reply to

    Re: FTP

    quote:
    Originally posted by herocomplex

    With the help of various other posts I've put together C# source code that works. Hopefully this will help others out there.



    I've been working with your code, and i'm wondering.. is it possible to open 1 connection, and then send multiple files before closing the connection.

    This would help me with the 'you can't login..' error.. as i think looping this whole code 10 times is too fast for the ftp client to handle... so if i could just login once, and then loop my 10 files.. that would be stellar.

    Thanks,
    Scott

    p.s. I realize how old this stuff is, but i'm hoping someone's still out there... :O)
  • 05-01-2008 10:49 In reply to

    Re: FTP

    You need to loop between the at the upload file command and maybe add a "sleep(0)" to the bottom of  the loop.

     

    My currently problem is I can't dispose of the streams in the correct order at the moment, I hate having to leave them for the GC to pickup when ever :( (as it is bad code design) esp when the streams are IDisposable based! :(

Page 4 of 4 (54 items) < Previous 1 2 3 4