Azure ftp uri无效

时间:2013-11-30 18:27:58

标签: c# asp.net azure ftp

我在azure上有一个网站,我必须使用asp.net将一些文件ftp到该服务器。

我正在使用以下代码抛出异常“请求的URI对此FTP命令无效。”

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();
            }

1 个答案:

答案 0 :(得分:1)

您需要在.Create()调用中指定要创建的远程文件的完整文件名,如下例所示:

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

参考文献:

通过FTP上传文件
http://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx

Azure中的FTP凭据(对我来说已经有一段时间了,所以我不得不重新考虑一下)
http://social.msdn.microsoft.com/Forums/windowsazure/en-us/4d3ced02-9893-4d4f-9bed-d6ac1cd10e65/whats-my-ftp-username-and-password?forum=windowsazuretroubleshooting