FTP FileUpload Error: An exception occurred during a WebClient request. InnerException: This method is not supported. (Parameter 'value')

FTP FileUpload Error: An exception occurred during a WebClient request. InnerException: This method is not supported. (Parameter 'value')

我正在尝试使用 WebClient 将文件从 IFormFile 上传到 FTPS。

private async Task SaveFileToFtp(IFormFile file)
{
    WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());

    using (var client = new WebClient())
    {
        client.Credentials = new NetworkCredential("username", "password");
        var address = "ftps://my-domain.com/folder/filename.txt";
        using Stream uploadStream = client.OpenWrite(address);
        using Stream fileStream = file.OpenReadStream();
        await fileStream.CopyToAsync(uploadStream);
    }
}

private sealed class FtpsWebRequestCreator : IWebRequestCreate
{
    public WebRequest Create(Uri uri)
    {
        // Removes the "s" in "ftps://".
        var requestUri = uri.AbsoluteUri.Remove(3, 1); 

        FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(requestUri); 
        webRequest.EnableSsl = true;
        return webRequest;
    }
}

我收到了这个错误: “WebClient 请求期间发生异常。InnerException:不支持此方法。(参数 'value')。”

谁能告诉我为什么?

谢谢

我已经改为使用 WinSCP 而不是 WebClient,它运行成功。

https://winscp.net/eng/docs/ui_generateurl#code