Reputation: 6654
I have a web site on azure and I have to ftp some files using asp.net to that server.
I am using following code which throws an exception "The requested URI is invalid for this FTP command."
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://abcd-prod-xyz-001.ftp.azurewebsites.windows.net/site/wwwroot/TestFolder/");
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.Timeout = -1;
ftpWebRequest.ReadWriteTimeout = -1;
ftpWebRequest.UsePassive = true;
ftpWebRequest.Credentials = new NetworkCredential("TestDomain\TestCred", "TestCred");
ftpWebRequest.KeepAlive = true;
ftpWebRequest.UseBinary = true;
try
{
using (Stream requestStream = ftpWebRequest.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
Upvotes: 0
Views: 529
Reputation: 466
Well you need to specify the full file name of the remote file to be created in the .Create() call like the following example:
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(@"ftp://sss-xxx-red-001.ftp.azurewebsites.windows.net/site/wwwroot/test.png");
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.Timeout = -1;
ftpWebRequest.ReadWriteTimeout = -1;
ftpWebRequest.UsePassive = true;
ftpWebRequest.Credentials = new NetworkCredential(@"myuser\$myuser", "myuser_password");
ftpWebRequest.KeepAlive = true;
ftpWebRequest.UseBinary = true;
try
{
using (Stream requestStream = ftpWebRequest.GetRequestStream())
{
byte[] fileContents = System.IO.File.ReadAllBytes(@"C:\_debug\test.png");
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
}
catch (Exception ex)
{
int i = 1;
}
References:
Uploading files via FTP
http://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx
FTP credentials in Azure (been a while for me so I had to figure that out again)
http://social.msdn.microsoft.com/Forums/windowsazure/en-us/4d3ced02-9893-4d4f-9bed-d6ac1cd10e65/whats-my-ftp-username-and-password?forum=windowsazuretroubleshooting
Upvotes: 1