【问题标题】:How can i create a directory on my ftp server before uploading a file to my ftp?在将文件上传到我的 ftp 之前,如何在我的 ftp 服务器上创建一个目录?
【发布时间】:2015-01-06 00:41:19
【问题描述】:

这是一个将文本文件上传到我的 ftp 根目录的工作代码。测试和工作。 但是现在我想在根目录上创建一个子目录,然后在创建目录后上传文件。

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

namespace mws
{
    class FtpFileUpload
    {
        static string ftpurl = "ftp://ftp.newsxpressmedia.com/";
        static string filename = @"c:\temp\FtpTestFile.txt";
        static string ftpusername = "Username";
        static string ftppassword = "Password";
        static string ftpdirectory = "subtestdir";

        public static void test()
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                ftpurl + "/" + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpusername, ftppassword);
                StreamReader sourceStream = new StreamReader(@"c:\temp\test1.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();
            }
        }
    }
}

我尝试更改第一行并添加了一行来创建目录:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                ftpurl + "/" + ftpdirectory + "/" + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.MakeDirectory;

然后我收到异常错误:

远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)。

但在更改之前它工作正常没有问题。 如何先在我的服务器上创建一个目录,然后将文件上传到创建的目录?

【问题讨论】:

标签: c# .net winforms ftp


【解决方案1】:

你检查过这个答案吗:How do I create a directory on ftp server using C#?
看起来您为 MakeDirectory 请求提供的路径实际上是文件名,而不是目录名。

【讨论】:

  • 你必须提出两个请求。正如我引用的问题的答案所建议的那样,您不能在一个请求中执行此操作。因此,您首先创建目录,然后上传文件。
  • 最后我使用了这个代码项目代码:codeproject.com/Tips/443588/Simple-Csharp-FTP-Class,我使用了我自己的文件上传方法,还添加了一些更改和选项,现在它工作得很好。
【解决方案2】:
public static void CreateDirectoryandSaveImage(string username, string password)
    {
       string ftphost = "ftp.placemyorder.com";
        string filePath = @"D:\ddlState.png";
        //You could not use "ftp://" + ftphost + "/FTPUPLOAD/WPF/WPF.txt" to create the folder,this will result the 550 error.
        string ftpfullpath = "ftp://" + ftphost + "/TestFolder";
    //    //Create the folder, Please notice if the WPF folder already exist, it will result 550 error.
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("ftp.placemyorder.com|placemyorder", "Cabbage123");
        ftp.UsePassive = true;
        ftp.UseBinary = true;
        ftp.KeepAlive = false;
        ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
       FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp.GetResponse();
        if (CreateForderResponse.StatusCode == FtpStatusCode.PathnameCreated)
        {
           //If folder created, upload file
            var fileName = Path.GetFileName(filePath);
            string ftpUploadFullPath = "ftp://" + ftphost + "/TestFolder/" + fileName;
            FtpWebRequest ftpUpLoadFile = (FtpWebRequest)FtpWebRequest.Create(ftpUploadFullPath);
            ftpUpLoadFile.KeepAlive = true;
           ftpUpLoadFile.UseBinary = true;
          ftpUpLoadFile.Method = WebRequestMethods.Ftp.UploadFile;
            ftpUpLoadFile.Credentials = new NetworkCredential(username, password);
            ftpUpLoadFile.UsePassive = true;
            ftpUpLoadFile.UseBinary = true;
            ftpUpLoadFile.KeepAlive = false;
           FileStream fs = File.OpenRead(filePath);
            byte[] buffer = new byte[fs.Length];
           fs.Read(buffer, 0, buffer.Length);
           fs.Close();
           Stream ftpstream = ftpUpLoadFile.GetRequestStream();
          ftpstream.Write(buffer, 0, buffer.Length);
           ftpstream.Close();
      }

    }

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多