Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2006

Tip: Looking for answers? Try searching our database.

FtpWebRequest for file upload

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arne - 19 Jan 2006 15:56 GMT
It is easy to understand who to download a file with FtpWebRequest.
How can I upload a file?
The data to be uploaded is currently in a string.

          request = WebRequest.Create(FTPlocation.Text)
           request.Credentials = New NetworkCredential(FTPAccount.Text,
FTPpassword.Text)

           Dim io As Stream
           io = request.GetRequestStream
           io.Write(out.ToString)    ' Does not  compile
Vadym Stetsyak - 19 Jan 2006 16:40 GMT
public static bool AppendFileOnServer(string fileName, Uri serverUri)
{
   // The URI described by serverUri should use the ftp:// scheme.
   // It contains the name of the file on the server.
   // Example: ftp://contoso.com/someFile.txt.
   // The fileName parameter identifies the file containing
   // the data to be appended to the file on the server.

   if (serverUri.Scheme != Uri.UriSchemeFtp)
   {
       return false;
   }
   // Get the object used to communicate with the server.
   FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
   request.Method = WebRequestMethods.Ftp.AppendFile;

   StreamReader sourceStream = new StreamReader(fileName);
   byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
   sourceStream.Close();
   request.ContentLength = fileContents.Length;

   // This example assumes the FTP site uses anonymous logon.
   request.Credentials = new NetworkCredential
("anonymous","janeDoe@contoso.com");
   Stream requestStream = request.GetRequestStream();
   requestStream.Write(fileContents, 0, fileContents.Length);
   requestStream.Close();
   FtpWebResponse response = (FtpWebResponse) request.GetResponse();

   Console.WriteLine("Append status: {0}",response.StatusDescription);

   response.Close();
   return true;
}

Signature

Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

> It is easy to understand who to download a file with FtpWebRequest.
> How can I upload a file?
[quoted text clipped - 7 lines]
>            io = request.GetRequestStream
>            io.Write(out.ToString)    ' Does not  compile
Arne - 19 Jan 2006 17:01 GMT
Vadym,

Will your code work with WebRequestMethods.Ftp.UploadFile also?

Arne

> public static bool AppendFileOnServer(string fileName, Uri serverUri)
> {
[quoted text clipped - 42 lines]
> >            io = request.GetRequestStream
> >            io.Write(out.ToString)    ' Does not  compile
Vadym Stetsyak - 19 Jan 2006 17:56 GMT
What stops you from checking if it works with
WebRequestMethods.Ftp.UploadFile?
I think that it will work :8-)

Signature

Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

> Vadym,
>
> Will your code work with WebRequestMethods.Ftp.UploadFile also?
>
> Arne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.