Пожалуйста, скажите мне, что не так в этом коде. FileService также выдает, что содержимое загруженного файла имеет нулевое значение или пусто, или формат URI не может быть определен. Класс Fileservice, похоже, работает для загрузки изображения, у которого есть другой метод. Учетные данные FTP также не кажутся неправильными. Тем не менее ошибка сохраняется. Я уже загрузил файл Performance.pdf на указанный ниже FTP-сервер, и этот путь, похоже, тоже работает.
FileService.cs:
public async Task GetFile(string fileName, CancellationToken cancellationToken)
{
if (String.IsNullOrEmpty(_ftpConfig.Hostname))
{
var filePath = $"{_ftpConfig.Hostname}/{fileName}"; byte[] byteData = File.ReadAllBytes(filePath); return byteData; }
var request = WebRequest.Create($"{_ftpConfig.Hostname}/{fileName}") as FtpWebRequest;
request.Credentials = new NetworkCredential(_ftpConfig.Username, _ftpConfig.Password);
request.Method = WebRequestMethods.Ftp.DownloadFile;
try
{
using var response = (FtpWebResponse)await request.GetResponseAsync();
using var responseStream = response.GetResponseStream();
using var memoryStream = new MemoryStream();
responseStream.CopyTo(memoryStream);
var bytes = memoryStream.ToArray();
return bytes;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Метод загрузки файла:
public async Task DownloadPerformanceAppraisal(CancellationToken cancellationToken)
{
var operation = new OperationResult(); string fileName = "Performance_Appraisal/Download/performance.pdf";
try
{
// Download file from FTP server using fileService
byte[] fileContent;
try
{
fileContent = await _fileService.GetFile(fileName, cancellationToken);
}
catch (Exception ex)
{
operation.Exception = new Exception("Error downloading file: " + ex.Message);
operation.Status = HttpStatusCode.InternalServerError;
return operation;
}
if (fileContent == null || fileContent.Length == 0)
{
operation.Exception = new Exception("Downloaded file content is null or empty.");
operation.Status = HttpStatusCode.InternalServerError;
return operation;
}
operation.Result = fileContent;
operation.Status = HttpStatusCode.OK;
}
catch (Exception ex)
{
await _errorLogService.LogException(operation, ex);
}
return operation;
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... explaining