Home All Groups Group Topic Archive Search About

FtpWebRequest for file upload

Author
19 Jan 2006 3:56 PM
Arne
It is easy to understand who to download a file with FtpWebRequest.
How can I upload a file?
The data to be uploaded is currently in a string.

           request = WebRequest.Create(FTPlocation.Text)
            request.Credentials = New NetworkCredential(FTPAccount.Text,
FTPpassword.Text)

            Dim io As Stream
            io = request.GetRequestStream
            io.Write(out.ToString)    ' Does not  compile

Author
19 Jan 2006 4:40 PM
Vadym Stetsyak
public static bool AppendFileOnServer(string fileName, Uri serverUri)
{
    // The URI described by serverUri should use the ftp:// scheme.
    // It contains the name of the file on the server.
    // Example: ftp://contoso.com/someFile.txt.
    // The fileName parameter identifies the file containing
    // the data to be appended to the file on the server.

    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.AppendFile;

    StreamReader sourceStream = new StreamReader(fileName);
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    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;
}

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

Show quote
"Arne" <A***@discussions.microsoft.com> wrote in message
news:59AE7C1C-09AF-4BCE-8F8E-E36C32443185@microsoft.com...
> It is easy to understand who to download a file with FtpWebRequest.
> How can I upload a file?
> The data to be uploaded is currently in a string.
>
>           request = WebRequest.Create(FTPlocation.Text)
>            request.Credentials = New NetworkCredential(FTPAccount.Text,
> FTPpassword.Text)
>
>            Dim io As Stream
>            io = request.GetRequestStream
>            io.Write(out.ToString)    ' Does not  compile
>
Author
19 Jan 2006 5:01 PM
Arne
Vadym,

Will your code work with WebRequestMethods.Ftp.UploadFile also?

Arne


Show quote
"Vadym Stetsyak" wrote:

> public static bool AppendFileOnServer(string fileName, Uri serverUri)
> {
>     // The URI described by serverUri should use the ftp:// scheme.
>     // It contains the name of the file on the server.
>     // Example: ftp://contoso.com/someFile.txt.
>     // The fileName parameter identifies the file containing
>     // the data to be appended to the file on the server.
>
>     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.AppendFile;
>
>     StreamReader sourceStream = new StreamReader(fileName);
>     byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
>     sourceStream.Close();
>     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;
> }
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "Arne" <A***@discussions.microsoft.com> wrote in message
> news:59AE7C1C-09AF-4BCE-8F8E-E36C32443185@microsoft.com...
> > It is easy to understand who to download a file with FtpWebRequest.
> > How can I upload a file?
> > The data to be uploaded is currently in a string.
> >
> >           request = WebRequest.Create(FTPlocation.Text)
> >            request.Credentials = New NetworkCredential(FTPAccount.Text,
> > FTPpassword.Text)
> >
> >            Dim io As Stream
> >            io = request.GetRequestStream
> >            io.Write(out.ToString)    ' Does not  compile
> >
>
>
>
Author
19 Jan 2006 5:56 PM
Vadym Stetsyak
What stops you from checking if it works with
WebRequestMethods.Ftp.UploadFile?
I think that it will work :8-)

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

Show quote
"Arne" <A***@discussions.microsoft.com> wrote in message
news:F8540E0A-A0C3-4E51-BA08-315402D5574B@microsoft.com...
> Vadym,
>
> Will your code work with WebRequestMethods.Ftp.UploadFile also?
>
> Arne
>

AddThis Social Bookmark Button