在将文件上传到ftp之前,我如何在ftp服务器上创建一个目录
本文关键字:ftp 创建 一个 服务器 文件 之前 | 更新日期: 2023-09-27 18:29:43
这是一个将文本文件上传到我的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#在ftp服务器上创建目录
看起来给MakeDirectory
请求的路径实际上是一个文件名,而不是目录名。
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();
}
}