Home All Groups Group Topic Archive Search About
Author
31 Jan 2006 1:48 PM
Arne
How can I upload some data to an FTP server, if the data is in a memory string?

Author
31 Jan 2006 2:29 PM
Vadym Stetsyak
Hello, Arne!

A> How can I upload some data to an FTP server, if the data is in a memory
A> string?

public static bool AppendFileOnServer(string memoryString, Uri serverUri)
{

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    byte [] fileContents = Encoding.UTF8.GetBytes(memoryString);
    request.ContentLength = fileContents.Length;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","jane***@contoso.com");
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();

    Console.WriteLine("Append status: {0}",response.StatusDescription);

    response.Close(); 
    return true;
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
31 Jan 2006 4:17 PM
Arne
Vadym,
AppendFile?
I was looking for an ftp PUT.

Show quote
"Vadym Stetsyak" wrote:

> Hello, Arne!
>
>  A> How can I upload some data to an FTP server, if the data is in a memory
>  A> string?
>
> public static bool AppendFileOnServer(string memoryString, Uri serverUri)
> {
>      
>     if (serverUri.Scheme != Uri.UriSchemeFtp)
>     {
>         return false;
>     }
>     // Get the object used to communicate with the server.
>     FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
>     request.Method = WebRequestMethods.Ftp.UploadFile;
>    
>     byte [] fileContents = Encoding.UTF8.GetBytes(memoryString);
>     request.ContentLength = fileContents.Length;

>     // This example assumes the FTP site uses anonymous logon.
>     request.Credentials = new NetworkCredential ("anonymous","jane***@contoso.com");
>     Stream requestStream = request.GetRequestStream();
>     requestStream.Write(fileContents, 0, fileContents.Length);
>     requestStream.Close();
>     FtpWebResponse response = (FtpWebResponse) request.GetResponse();
>    
>     Console.WriteLine("Append status: {0}",response.StatusDescription);
>    
>     response.Close(); 
>     return true;
> }
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot
Author
31 Jan 2006 4:43 PM
Vadym Stetsyak
Hello, Arne!

Take a look at line with the following code

request.Method = WebRequestMethods.Ftp.UploadFile;

You can change the name of the sample method on whatever you want.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

AddThis Social Bookmark Button