2014-09-22 158 views
1

我需要从本地机器复制一些txt文件到FTP服务器。我使用下面的代码。 (从了:http://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx从本地机器复制文件到FTP服务器c

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxxx.com/"); 

    request.Method = WebRequestMethods.Ftp.UploadFile; 

    // This example assumes the FTP site uses anonymous logon. 
    request.Credentials = new NetworkCredential("username", "paswword"); 

    // Copy the contents of the file to the request stream. 
    StreamReader sourceStream = new StreamReader(@"E:\log.txt"); 
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
    sourceStream.Close(); 
    request.ContentLength = fileContents.Length; 

    Stream requestStream = request.GetRequestStream(); 

    requestStream.Write(fileContents, 0, fileContents.Length); 
    requestStream.Close(); 

    FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

    response.Close(); 
    } 

但我的事业 “的请求URI是无效的这个FTP命令” 的错误。我怎么解决这个问题?

回答

0

您在URI中缺少文件名。例如:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxxx.com/log.txt"); 

请仔细看看您使用的示例。注意test.htm

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"); 
相关问题