FtpWebRequest for file upload

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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","(e-mail address removed)");
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;
}
 
Vadym,

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

Arne


Vadym Stetsyak said:
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","(e-mail address removed)");
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;
}

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

Arne said:
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
 
What stops you from checking if it works with
WebRequestMethods.Ftp.UploadFile?
I think that it will work :8-)
 
Back
Top