【问题标题】:How do i upload a file to my ftp at weebly.com?如何在 weebly.com 上将文件上传到我的 ftp?
【发布时间】:2014-03-22 11:39:00
【问题描述】:

这是他们所说的使用 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();

【问题讨论】:

标签: c# ftp


【解决方案1】:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
    ftpUrl + "/" + Path.GetFileName(fileName));

您需要包含文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2014-10-17
    • 2015-01-06
    • 2012-03-12
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多