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