如何在weebly.com上传文件到我的ftp?

时间:2014-03-22 11:39:01

标签: c# ftp

这就是他们对使用ftp的看法:

http://www.ipage.com/controlpanel/FTP.bml

您可以使用Web创作工具构建您的网站,然后使用FTP程序将网页上传到您的iPage帐户。

要使用FTP客户端访问您的帐户,您需要使用您的FTP用户名和密码连接到ftp.newsxpressmedia.com。

这是我现在尝试的方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace ScrollLabelTest
{
    class FtpFileUploader
    {
        static string ftpurl = "ftp://ftp.newsxpressmedia.com";
        static string filename = @"c:\temp\test.txt";
        static string ftpusername = "newsxpressmediacom";
        static string ftppassword = "*****";
        static string value;

        public static void test()
        {
            try
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpurl);
                request.Method = WebRequestMethods.Ftp.UploadFile;

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

                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(@"c:\temp\test.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();
            }
            catch(Exception err)
            {
                string t = err.ToString();
            }
        }
    }
}

但我在线上获得例外:

Stream requestStream = request.GetRequestStream();

例外:

The requested URI is invalid for this FTP command

完整的异常错误消息:

System.Net.WebException was caught
  HResult=-2146233079
  Message=The requested URI is invalid for this FTP command.
  Source=System
  StackTrace:
       at System.Net.FtpWebRequest.GetRequestStream()
       at ScrollLabelTest.FtpFileUploader.test() in e:\test\test\test\FtpFileUploader.cs:line 36
  InnerException: 

第36行是:

Stream requestStream = request.GetRequestStream();

1 个答案:

答案 0 :(得分:2)

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
    ftpUrl + "/" + Path.GetFileName(fileName));

您需要包含文件名。