FtpWebRequest UploadFile

T

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?
 
S

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" <[email protected]>
| 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: <#[email protected]>
| 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.
| --
|
|
|
|
 
T

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

Steven Cheng said:
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" <[email protected]>
| 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: <#[email protected]>
| 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.
| --
|
|
|
|
 
S

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" <[email protected]>
| References: <#[email protected]>
<[email protected]>
| 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: <[email protected]>
| 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
| 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.
| --
|
| | > 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" <[email protected]>
| > | 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: <#[email protected]>
| > | 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.
| > | --
| > |
| > |
| > |
| > |
| >
|
|
|
 
T

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.
 
S

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" <[email protected]>
| References: <#[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
| 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: <[email protected]>
| 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
| 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.
| --
|
| | > 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.)
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top