Home All Groups Group Topic Archive Search About
Author
13 Nov 2005 10:45 PM
Thom Little
I posted the WebClient versions in this Newsgroups.

The FtpWebRequest DownloadFile version is ...

  private byte[] byWork = new byte[2047] ;
  private int iWork ;

  private void DownloadFile(string strAddress, string strUsername, string
strPassword )
   {
   this.lblStatus.Visible = false ;
   try
    {
    FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
strAddress );
    req.Credentials = new NetworkCredential(strUsername, strPassword );
    req.Method = WebRequestMethods.Ftp.DownloadFile ;
    FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
    Stream rs = resp.GetResponseStream( );
    FileStream fs = new FileStream(this.tbAddressClient.Text.ToString(),
FileMode.Create );
    do {
     iWork = rs.Read(byWork, 0, byWork.Length);
     fs.Write(byWork, 0, iWork);
     } while (iWork != 0);
    fs.Flush();
    fs.Close();
    rs.Close();
    resp.Close();
    this.lblStatus.Text = "File Download - Success";
    }
   catch
    {
    this.lblStatus.Text = "File Download - Failed" ;
    }
   this.lblStatus.Visible = true ;
   }

Has anyone seen a simple working FtpWebRequest UploadFile example?

--
--  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
--

Author
14 Nov 2005 10:04 AM
Steven Cheng[MSFT]
Hi Thom,

Welcome to ASPNET newsgroup.
As for the FtpWebRequest class, do you mean the one in the .NET framework
2.0 's  build-in class library? If so, here is a simple code snippet which
upload a file to the local FTP server(with anonymous access turn on):

======================
private void UploadFile()
        {
            ManualResetEvent waitObject;

            Uri target = new Uri("ftp://localhost/testfile.cs");
            string fileName = "c:\myproject\test.cs";

            FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(target);
            request.Method = WebRequestMethods.Ftp.UploadFile;



            Stream requestStream = request.GetRequestStream();

            FileStream fs = File.Open(fileName, FileMode.Open);

            byte[] buf = new byte[1024];

            int i;

            while((i=fs.Read(buf,0,buf.Length))>0)
            {
                requestStream.Write(buf, 0, i);
            }

            requestStream.Close();

            FtpWebResponse response = request.GetResponse() as
FtpWebResponse;

            MessageBox.Show(response.StatusDescription);

            response.Close();

        }

======================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Thom Little" <t***@tlanet.net>
| Subject: FtpWebRequest UploadFile
| Date: Sun, 13 Nov 2005 17:45:57 -0500
| Lines: 46
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 65.99.185.176
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:31122
Show quoteHide quote
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| I posted the WebClient versions in this Newsgroups.
|
| The FtpWebRequest DownloadFile version is ...
|
|   private byte[] byWork = new byte[2047] ;
|   private int iWork ;
|
|   private void DownloadFile(string strAddress, string strUsername, string
| strPassword )
|    {
|    this.lblStatus.Visible = false ;
|    try
|     {
|     FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
| strAddress );
|     req.Credentials = new NetworkCredential(strUsername, strPassword );
|     req.Method = WebRequestMethods.Ftp.DownloadFile ;
|     FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
|     Stream rs = resp.GetResponseStream( );
|     FileStream fs = new FileStream(this.tbAddressClient.Text.ToString(),
| FileMode.Create );
|     do {
|      iWork = rs.Read(byWork, 0, byWork.Length);
|      fs.Write(byWork, 0, iWork);
|      } while (iWork != 0);
|     fs.Flush();
|     fs.Close();
|     rs.Close();
|     resp.Close();
|     this.lblStatus.Text = "File Download - Success";
|     }
|    catch
|     {
|     this.lblStatus.Text = "File Download - Failed" ;
|     }
|    this.lblStatus.Visible = true ;
|    }
|
| Has anyone seen a simple working FtpWebRequest UploadFile example?
|
| --
| --  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
| --

|
|
|
Author
14 Nov 2005 3:31 PM
Thom Little
This isn't quite what I needed.  This is EXACTLY what I needed.

The input stream needs to be closed.

What is the advantage in defining a Uri that used once vs. just using the
string without the Uri creation?

Thank you for the help.  (My original attempt had a bug that I found after
seeing yours solution.)

--
--  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
--

Show quoteHide quote
"Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
news:y3v7VLQ6FHA.832@TK2MSFTNGXA02.phx.gbl...
> Hi Thom,
>
> Welcome to ASPNET newsgroup.
> As for the FtpWebRequest class, do you mean the one in the .NET framework
> 2.0 's  build-in class library? If so, here is a simple code snippet which
> upload a file to the local FTP server(with anonymous access turn on):
>
> ======================
> private void UploadFile()
>        {
>            ManualResetEvent waitObject;
>
>            Uri target = new Uri("ftp://localhost/testfile.cs");
>            string fileName = "c:\myproject\test.cs";
>
>            FtpWebRequest request =
> (FtpWebRequest)WebRequest.Create(target);
>            request.Method = WebRequestMethods.Ftp.UploadFile;
>
>
>
>            Stream requestStream = request.GetRequestStream();
>
>            FileStream fs = File.Open(fileName, FileMode.Open);
>
>            byte[] buf = new byte[1024];
>
>            int i;
>
>            while((i=fs.Read(buf,0,buf.Length))>0)
>            {
>                requestStream.Write(buf, 0, i);
>            }
>
>            requestStream.Close();
>
>            FtpWebResponse response = request.GetResponse() as
> FtpWebResponse;
>
>            MessageBox.Show(response.StatusDescription);
>
>            response.Close();
>
>        }
>
> ======================
>
> Hope helps. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
> --------------------
> | From: "Thom Little" <t***@tlanet.net>
> | Subject: FtpWebRequest UploadFile
> | Date: Sun, 13 Nov 2005 17:45:57 -0500
> | Lines: 46
> | X-Priority: 3
> | X-MSMail-Priority: Normal
> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
> | X-RFC2646: Format=Flowed; Original
> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
> | Message-ID: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl>
> | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
> | NNTP-Posting-Host: 65.99.185.176
> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
> | Xref: TK2MSFTNGXA02.phx.gbl
> microsoft.public.dotnet.framework.aspnet.webcontrols:31122
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
> |
> | I posted the WebClient versions in this Newsgroups.
> |
> | The FtpWebRequest DownloadFile version is ...
> |
> |   private byte[] byWork = new byte[2047] ;
> |   private int iWork ;
> |
> |   private void DownloadFile(string strAddress, string strUsername,
> string
> | strPassword )
> |    {
> |    this.lblStatus.Visible = false ;
> |    try
> |     {
> |     FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
> | strAddress );
> |     req.Credentials = new NetworkCredential(strUsername, strPassword );
> |     req.Method = WebRequestMethods.Ftp.DownloadFile ;
> |     FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
> |     Stream rs = resp.GetResponseStream( );
> |     FileStream fs = new FileStream(this.tbAddressClient.Text.ToString(),
> | FileMode.Create );
> |     do {
> |      iWork = rs.Read(byWork, 0, byWork.Length);
> |      fs.Write(byWork, 0, iWork);
> |      } while (iWork != 0);
> |     fs.Flush();
> |     fs.Close();
> |     rs.Close();
> |     resp.Close();
> |     this.lblStatus.Text = "File Download - Success";
> |     }
> |    catch
> |     {
> |     this.lblStatus.Text = "File Download - Failed" ;
> |     }
> |    this.lblStatus.Visible = true ;
> |    }
> |
> | Has anyone seen a simple working FtpWebRequest UploadFile example?
> |
> | --
> | --  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
> | --
> |
> |
> |
> |
>
Author
15 Nov 2005 2:33 AM
Steven Cheng[MSFT]
Thanks for your respones Thom,

Yes, the FileStream should be closed which I missed. As for using Uri class
or directly string operation, it is another story which somewhat related to
the concept of URI (URN and URL) , and here,  there is no critical concerns
on using Uri or just string path.  Sometimes, when we don't know the path
string in advance(dynamically generate URL from string...), using Uri class
can help avoid including some invalid chars which may cause security issue.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Thom Little" <t***@tlanet.net>
| References: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl>
<y3v7VLQ6FHA.***@TK2MSFTNGXA02.phx.gbl>
| Subject: Re: FtpWebRequest UploadFile
| Date: Mon, 14 Nov 2005 10:31:18 -0500
| Lines: 143
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <OCru5BT6FHA.3***@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 65.99.185.176
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:31134
Show quoteHide quote
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| This isn't quite what I needed.  This is EXACTLY what I needed.
|
| The input stream needs to be closed.
|
| What is the advantage in defining a Uri that used once vs. just using the
| string without the Uri creation?
|
| Thank you for the help.  (My original attempt had a bug that I found
after
| seeing yours solution.)
|
| --
| --  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
| --
|
| "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
| news:y3v7VLQ6FHA.832@TK2MSFTNGXA02.phx.gbl...
| > Hi Thom,
| >
| > Welcome to ASPNET newsgroup.
| > As for the FtpWebRequest class, do you mean the one in the .NET
framework
| > 2.0 's  build-in class library? If so, here is a simple code snippet
which
| > upload a file to the local FTP server(with anonymous access turn on):
| >
| > ======================
| > private void UploadFile()
| >        {
| >            ManualResetEvent waitObject;
| >
| >            Uri target = new Uri("ftp://localhost/testfile.cs");
| >            string fileName = "c:\myproject\test.cs";
| >
| >            FtpWebRequest request =
| > (FtpWebRequest)WebRequest.Create(target);
| >            request.Method = WebRequestMethods.Ftp.UploadFile;
| >
| >
| >
| >            Stream requestStream = request.GetRequestStream();
| >
| >            FileStream fs = File.Open(fileName, FileMode.Open);
| >
| >            byte[] buf = new byte[1024];
| >
| >            int i;
| >
| >            while((i=fs.Read(buf,0,buf.Length))>0)
| >            {
| >                requestStream.Write(buf, 0, i);
| >            }
| >
| >            requestStream.Close();
| >
| >            FtpWebResponse response = request.GetResponse() as
| > FtpWebResponse;
| >
| >            MessageBox.Show(response.StatusDescription);
| >
| >            response.Close();
| >
| >        }
| >
| > ======================
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | From: "Thom Little" <t***@tlanet.net>
| > | Subject: FtpWebRequest UploadFile
| > | Date: Sun, 13 Nov 2005 17:45:57 -0500
| > | Lines: 46
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | Message-ID: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: 65.99.185.176
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:31122
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | I posted the WebClient versions in this Newsgroups.
| > |
| > | The FtpWebRequest DownloadFile version is ...
| > |
| > |   private byte[] byWork = new byte[2047] ;
| > |   private int iWork ;
| > |
| > |   private void DownloadFile(string strAddress, string strUsername,
| > string
| > | strPassword )
| > |    {
| > |    this.lblStatus.Visible = false ;
| > |    try
| > |     {
| > |     FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
| > | strAddress );
| > |     req.Credentials = new NetworkCredential(strUsername, strPassword
);
| > |     req.Method = WebRequestMethods.Ftp.DownloadFile ;
| > |     FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
| > |     Stream rs = resp.GetResponseStream( );
| > |     FileStream fs = new
FileStream(this.tbAddressClient.Text.ToString(),
Show quoteHide quote
| > | FileMode.Create );
| > |     do {
| > |      iWork = rs.Read(byWork, 0, byWork.Length);
| > |      fs.Write(byWork, 0, iWork);
| > |      } while (iWork != 0);
| > |     fs.Flush();
| > |     fs.Close();
| > |     rs.Close();
| > |     resp.Close();
| > |     this.lblStatus.Text = "File Download - Success";
| > |     }
| > |    catch
| > |     {
| > |     this.lblStatus.Text = "File Download - Failed" ;
| > |     }
| > |    this.lblStatus.Visible = true ;
| > |    }
| > |
| > | Has anyone seen a simple working FtpWebRequest UploadFile example?
| > |
| > | --
| > | --  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
| > | --
| > |
| > |
| > |
| > |
| >
|
|
|
Author
15 Nov 2005 12:35 PM
Thom Little
The final "operate on a file" version that I came up with (with your ideas
folded in) are ...

  private void DownloadFile( string strFilename, string strAddress, string
strUsername, string strPassword )
   {
   byte[] buf = new byte[2047];
   int iWork ;
   FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
strAddress + strFilename );
   req.Credentials = new NetworkCredential(strUsername, strPassword );
   req.Method = WebRequestMethods.Ftp.DownloadFile ;
   FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
   Stream rs = resp.GetResponseStream( );
   FileStream fs = new FileStream(this.tbAddressClient.Text.ToString() +
strFilename, FileMode.Create);
   while ( ( iWork = rs.Read( buf, 0, buf.Length ) ) > 0 )
    fs.Write( buf, 0, iWork );
   fs.Close();
   rs.Close();
   }

  private void UploadFile( string strFilename, string strAddress, string
strUsername, string strPassword )
   {
   byte[] buf = new byte[2048] ;
   int iWork ;
   FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
strAddress + strFilename);
   req.Credentials = new NetworkCredential(strUsername, strPassword);
   req.Method = WebRequestMethods.Ftp.UploadFile ;
   Stream rs = req.GetRequestStream( );
   FileStream fs = File.Open(this.tbAddressClient.Text.ToString() +
strFilename, FileMode.Open);
   while ( ( iWork = fs.Read( buf, 0, buf.Length ) ) > 0 )
    rs.Write( buf, 0, iWork );
   rs.Close();
   fs.Close();
   }

.... thanks for the help.

My next (final) step is to come up with the "operate on a folder" version.

--
--  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
--

Show quoteHide quote
"Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
news:%234UPI0Y6FHA.1240@TK2MSFTNGXA02.phx.gbl...
> Thanks for your respones Thom,
>
> Yes, the FileStream should be closed which I missed. As for using Uri
> class
> or directly string operation, it is another story which somewhat related
> to
> the concept of URI (URN and URL) , and here,  there is no critical
> concerns
> on using Uri or just string path.  Sometimes, when we don't know the path
> string in advance(dynamically generate URL from string...), using Uri
> class
> can help avoid including some invalid chars which may cause security
> issue.
>
> Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Author
15 Nov 2005 1:08 PM
Steven Cheng[MSFT]
Cool! And thanks for sharing this with us.

Also, please feel free to post here when you've finished other parts or if
any thing need asistance.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Thom Little" <t***@tlanet.net>
| References: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl>
<y3v7VLQ6FHA.***@TK2MSFTNGXA02.phx.gbl>
<OCru5BT6FHA.3***@TK2MSFTNGP09.phx.gbl>
<#4UPI0Y6FHA.1***@TK2MSFTNGXA02.phx.gbl>
| Subject: Re: FtpWebRequest UploadFile
| Date: Tue, 15 Nov 2005 07:35:51 -0500
| Lines: 74
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <u7RxhEe6FHA.3***@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 65.99.185.176
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:31143
Show quoteHide quote
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| The final "operate on a file" version that I came up with (with your
ideas
| folded in) are ...
|
|   private void DownloadFile( string strFilename, string strAddress,
string
| strUsername, string strPassword )
|    {
|    byte[] buf = new byte[2047];
|    int iWork ;
|    FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
| strAddress + strFilename );
|    req.Credentials = new NetworkCredential(strUsername, strPassword );
|    req.Method = WebRequestMethods.Ftp.DownloadFile ;
|    FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
|    Stream rs = resp.GetResponseStream( );
|    FileStream fs = new FileStream(this.tbAddressClient.Text.ToString() +
| strFilename, FileMode.Create);
|    while ( ( iWork = rs.Read( buf, 0, buf.Length ) ) > 0 )
|     fs.Write( buf, 0, iWork );
|    fs.Close();
|    rs.Close();
|    }
|
|   private void UploadFile( string strFilename, string strAddress, string
| strUsername, string strPassword )
|    {
|    byte[] buf = new byte[2048] ;
|    int iWork ;
|    FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://" +
| strAddress + strFilename);
|    req.Credentials = new NetworkCredential(strUsername, strPassword);
|    req.Method = WebRequestMethods.Ftp.UploadFile ;
|    Stream rs = req.GetRequestStream( );
|    FileStream fs = File.Open(this.tbAddressClient.Text.ToString() +
| strFilename, FileMode.Open);
|    while ( ( iWork = fs.Read( buf, 0, buf.Length ) ) > 0 )
|     rs.Write( buf, 0, iWork );
|    rs.Close();
|    fs.Close();
|    }
|
| ... thanks for the help.
|
| My next (final) step is to come up with the "operate on a folder" version.
|
| --
| --  Thom Little  --  www.tlanet.net  --  Thom Little Associates, Ltd.
| --
|
| "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
| news:%234UPI0Y6FHA.1240@TK2MSFTNGXA02.phx.gbl...
| > Thanks for your respones Thom,
| >
| > Yes, the FileStream should be closed which I missed. As for using Uri
| > class
| > or directly string operation, it is another story which somewhat
related
| > to
| > the concept of URI (URN and URL) , and here,  there is no critical
| > concerns
| > on using Uri or just string path.  Sometimes, when we don't know the
path
| > string in advance(dynamically generate URL from string...), using Uri
| > class
| > can help avoid including some invalid chars which may cause security
| > issue.
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
|
|
|