Re: FtpWebRequest UploadFile

Tech-Archive recommends: Fix windows errors by optimizing your registry



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" <thom@xxxxxxxxxx>
| References: <#grFHQK6FHA.1276@xxxxxxxxxxxxxxxxxxxx>
<y3v7VLQ6FHA.832@xxxxxxxxxxxxxxxxxxxxx>
| 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.3636@xxxxxxxxxxxxxxxxxxxx>
| 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.
| --
|
| "Steven Cheng[MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
| news:y3v7VLQ6FHA.832@xxxxxxxxxxxxxxxxxxxxxxxx
| > 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" <thom@xxxxxxxxxx>
| > | 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.1276@xxxxxxxxxxxxxxxxxxxx>
| > | 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.
| > | --
| > |
| > |
| > |
| > |
| >
|
|
|

.



Relevant Pages

  • RE: MSVSTO.Applications.Runtime.IEntryPoint vs multiple verions is
    ... FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean ... at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, ... the assembies ref to error ...
    (microsoft.public.office.developer.automation)
  • Re: Put document and FileName
    ... If you want to write in Spanish, post SharePoint questions to microsoft.public.es.sharepoint. ... SipaAdmElectIntegracion.SharePointFileUploader.SendRequest(String uri, ... String& webUrl, String& fileUrl) ... public void PutDocument(string uri, bytebFichero, string metaInfo) ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: URL encoding api in Java 1.4.2
    ... reserved characters in URI syntax, ... Append the resulting string to the submission URL, ... dubious about the interaction between x-www-form-urlencoding and URI ...   If the method is "get" and the action is an HTTP URI, ...
    (comp.lang.java.programmer)
  • Re: Physical vs Logical My Documents question
    ... The Uri.MakeRelative function "determines the difference between two Uri ... Public Function IsRoot(ByVal rootPath As String, ... Documents\Visual Studio Projects" ... | Is there some built in way to know whether a physical folder path is ...
    (microsoft.public.dotnet.languages.vb)
  • Re: UTF-8 problems with windows
    ... functional differences between URIs generally and URLs specifically. ... URI (which is a parser for a String) and a URL (which is an access ... which the URI class rejects but the URL class ...
    (comp.lang.java.programmer)