|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FtpWebRequest UploadFileThe 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? 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> microsoft.public.dotnet.framework.aspnet.webcontrols:31122| 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 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. | -- | | | | 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.) 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. > | -- > | > | > | > | > 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> <y3v7VLQ6FHA.***@TK2MSFTNGXA02.phx.gbl>| References: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl> | Subject: Re: FtpWebRequest UploadFile microsoft.public.dotnet.framework.aspnet.webcontrols:31134| 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 Show quoteHide quote | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols FileStream(this.tbAddressClient.Text.ToString(),| | 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 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. | > | -- | > | | > | | > | | > | | > | | | 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. 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.) 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> <y3v7VLQ6FHA.***@TK2MSFTNGXA02.phx.gbl> | References: <#grFHQK6FHA.1***@TK2MSFTNGP09.phx.gbl> <OCru5BT6FHA.3***@TK2MSFTNGP09.phx.gbl> <#4UPI0Y6FHA.1***@TK2MSFTNGXA02.phx.gbl> | Subject: Re: FtpWebRequest UploadFile microsoft.public.dotnet.framework.aspnet.webcontrols:31143| 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 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.) | | |
Problems with Re-Usable ASP.NET User Control Libraries
WebCLient Web Server Control and property tree in design time like Font has. Retrieve Datagrid Element Index by Name Retrieve GridView column header text on sort how to override DropDownList.SelectedValue property DropDownList Box cannot access container data in templated control Webcontrol custom designer Newbie Listbox |
|||||||||||||||||||||||