Uploading a text file to a server

D

dawson

Hello, when running the code below, it returns the error message:

"The requested URI is invalid for this FTP command.".

I have confirmed the FTP URI is correct and working through a
standalone FTP client as well as windows explorer, the domain\username
and password are also valid auths.

I've also downloaded two seperate classes similiar to the code below,
from MSDN, and I'm getting this same error message whatever I do. Any
ideas? -- Thanks.

protected void Upload(string fileName)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =

(FtpWebRequest)WebRequest.Create("ftp://ftp_web.mydomain.com");
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Credentials = new
NetworkCredential(@"domain\username", "password");

// UploadFile is not supported through an Http proxy
// so we disable the proxy for this request.
uploadRequest.Proxy = null;

requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);

byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}

// The request stream must be closed before getting
// the response.
requestStream.Close();

uploadResponse =
(FtpWebResponse)uploadRequest.GetResponse();
lblPublishResult.Text = "Content published live " +
System.DateTime.Now.ToString();
}
catch (UriFormatException ex)
{
lblPublishResult.Text = ex.Message.ToString();
}
catch (IOException ex)
{
lblPublishResult.Text = ex.Message.ToString();
}
catch (WebException ex)
{
lblPublishResult.Text = ex.Message.ToString();
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}
 
D

dawson

I needed to specify the name of the destination file when you upload,
as in:

FtpWebRequest uploadRequest = (FtpWebRequest) WebRequest.Create
("ftp://ftp_web.mydomain.com/destination.txt");
 

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