如何在c#中创建一个任务,用FTP上传文件

本文关键字:任务 一个 FTP 文件 创建 | 更新日期: 2023-09-27 18:07:50

我注意到蛋糕支持HTTP操作,但不支持FTP操作,你知道如何创建一个任务来通过FTP上传文件吗?

如何在c#中创建一个任务,用FTP上传文件

目前Cake中还没有提供使用FTP协议传输文件的功能。

虽然如果你在"全CLR"上运行Cake,你可以使用FtpWebRequest内置的。net框架来上传文件。FtpWebRequest还没有被移植到.NET Core,所以如果你正在运行Cake.CoreCLR

您可以通过使用静态FTPUpload实用程序方法创建ftp.cake来实现此目的,您可以从build.cake文件中重用该方法。

例子ftp.cake

public static bool FTPUpload(
    ICakeContext context,
    string ftpUri,
    string user,
    string password,
    FilePath filePath,
    out string uploadResponseStatus
    )
{
    if (context==null)
    {
        throw new ArgumentNullException("context");
    }
    if (string.IsNullOrEmpty(ftpUri))
    {
        throw new ArgumentNullException("ftpUri");
    }
    if (string.IsNullOrEmpty(user))
    {
        throw new ArgumentNullException("user");
    }
    if (string.IsNullOrEmpty(password))
    {
        throw new ArgumentNullException("password");
    }
    if (filePath==null)
    {
        throw new ArgumentNullException("filePath");
    }
    if (!context.FileSystem.Exist(filePath))
    {
        throw new System.IO.FileNotFoundException("Source file not found.", filePath.FullPath);
    }
    uploadResponseStatus = null;
    var ftpFullPath = string.Format(
        "{0}/{1}",
        ftpUri.TrimEnd('/'),
        filePath.GetFilename()
        );
    var ftpUpload = System.Net.WebRequest.Create(ftpFullPath) as System.Net.FtpWebRequest;
    if (ftpUpload == null)
    {
        uploadResponseStatus = "Failed to create web request";
        return false;
    }
    ftpUpload.Credentials = new System.Net.NetworkCredential(user, password);
    ftpUpload.KeepAlive = false;
    ftpUpload.UseBinary = true;
    ftpUpload.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
    using (System.IO.Stream
        sourceStream = context.FileSystem.GetFile(filePath).OpenRead(),
        uploadStream = ftpUpload.GetRequestStream())
    {
        sourceStream.CopyTo(uploadStream);
        uploadStream.Close();
    }
    var uploadResponse = (System.Net.FtpWebResponse)ftpUpload.GetResponse();
    uploadResponseStatus = (uploadResponse.StatusDescription ?? string.Empty).Trim().ToUpper();
    uploadResponse.Close();
    return  uploadResponseStatus.Contains("TRANSFER COMPLETE") ||
                     uploadResponseStatus.Contains("FILE RECEIVE OK");
}

示例用法
#load "ftp.cake"
string ftpPath = "ftp://ftp.server.com/test";
string ftpUser = "john";
string ftpPassword = "top!secret";
FilePath sourceFile = File("./data.zip");

Information("Uploading to upload {0} to {1}...", sourceFile, ftpPath);
string uploadResponseStatus;
if (!FTPUpload(
        Context,
        ftpPath,
        ftpUser,
        ftpPassword,
        sourceFile,
        out uploadResponseStatus
    ))
{
    throw new Exception(string.Format(
        "Failed to upload {0} to {1} ({2})",
        sourceFile,
        ftpPath,
        uploadResponseStatus));
}
Information("Successfully uploaded file ({0})", uploadResponseStatus);

示例输出

Uploading to upload log.cake to ftp://ftp.server.com/test...
Successfully uploaded file (226 TRANSFER COMPLETE.)

FtpWebRequest是非常基本的,所以你可能需要调整它以适应你的目标ftp服务器,但以上应该是一个很好的起点。

虽然我自己没有尝试过,但我"认为"我说你可以使用这个蛋糕插件进行文件传输操作是对的。

如果没有,最好的方法是为Cake创建一个自定义插件,它提供了您正在寻找的功能。

有一个问题是,这是否应该成为蛋糕的核心功能集,然而,首先要做的是一个插件。