FtpWebRequest for file upload

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
 
V

Vadym Stetsyak

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;
}
 
G

Guest

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
 
V

Vadym Stetsyak

What stops you from checking if it works with
WebRequestMethods.Ftp.UploadFile?
I think that it will work ::cool:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top