Home > database >  FTP FileUpload Error: An exception occurred during a WebClient request. InnerException: This method
FTP FileUpload Error: An exception occurred during a WebClient request. InnerException: This method

Time:03-15

I'm trying to upload a file from IFormFile to FTPS using WebClient.

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;
    }
}

And I'm getting this error: "An exception occurred during a WebClient request. InnerException: This method is not supported. (Parameter 'value')."

Can anyone tell me why?

Thanks

CodePudding user response:

I've changed to use WinSCP instead of WebClient, and it works successfully.

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

  • Related