I have a method, where I use the deprecated WebClient
class to upload a file to a FTP Server
.
public async Task<BaseResponse<Void>> Handle(FTPUploadRequest request, CancellationToken cancellationToken)
{
using WebClient client = new()
{
Credentials = new NetworkCredential(configuration.FTP.User, configuration.FTP.Password.Decrypt())
};
static string BuildUri(FTPUploadRequest request, IOptionsSnapshot<Configuration> configuration)
=> $"ftp://{configuration.FTP.Server}/{request.DirectoryPath}/{request.FileName}";
client.UploadFile(
BuildUri(request, configuration),
WebRequestMethods.Ftp.UploadFile,
request.FilePath + "/" + request.FileName);
return await BaseResponse<Void>.Create(true);
}
WebClient
is deprecated and not Unit-testable
. So I wanted to inject IHttpClientFactory
which is also stated in the deprecation-message. I can easily do that, but how should I upload the file? Posting against a FTP Server
with a multi-part-message
?
I have no idea and cannot find any solution on the internet. Downloading from a FTP Server is more easy by just using Get
and having the file path with basic authentication
. But uploading is more challenging.
Any Ideas?