public async Task <byte[]> DownloadAsync(string Path, string Filename) { try { if (await _ftpClient.DirectoryExistsAsync(Path)) { _ftpClient.RetryAttempts = 10; string fullpath = Path + Filename; using (MemoryStream ms = new MemoryStream()) { _log.Info($"{FtpTemplateMessage("Download", Filename, "Downloading")}", fullpath); await _ftpClient.DownloadAsync(ms, fullpath, 0); return(ms.ToArray()); } } else { _log.Info($"{FtpTemplateMessage("Download", Filename, "Not Exist")}", Path + Filename); throw new Exception("File Not Exist"); } } catch (Exception ex) { _log.Error($"{FtpTemplateMessage("Download", Filename, "throw exception")}", ex); throw ex; } }
private static async Task DownloadAsync(IFtpClient client, string fileName, Stream stream, CancellationToken ct) { try { await client.DownloadAsync(stream, fileName, token : ct); } catch (FtpException ex) when(IsNotFound(ex)) { throw new AssetNotFoundException(fileName, ex); } }
public async Task <Stream> Read(string fileName) { await _EnsureConnection(); var stream = new MemoryStream(); await _ftpClient.DownloadAsync(stream, $"/{PathHelper.Combine(_options.Path, fileName)}"); stream.Seek(0, SeekOrigin.Begin); return(stream); }
protected virtual async Task <ImportVehiclesForSiteResult> InternalImportForSiteAsync(Site site, IFtpClient ftpClient) { ImportVehiclesForSiteResult importResult; try { if (site.ImportRelativeFtpPath != null) { if (ftpClient.DirectoryExists(site.ImportRelativeFtpPath)) { FtpListItem lastModifiedFileInfo = (await ftpClient.GetListingAsync(site.ImportRelativeFtpPath, FtpListOption.Auto)) .Where(r => r.Type == FtpFileSystemObjectType.File) .OrderByDescending(r => r.Modified) .FirstOrDefault(); if (lastModifiedFileInfo != null) { IEnumerable <Vehicle> vehicles; using (var csvFileStream = new MemoryStream(await ftpClient.DownloadAsync(lastModifiedFileInfo.FullName))) { vehicles = VehicleBulkFactory.Create(new VehicleFromCsvFileBulkFactorySettings(site.Id, csvFileStream)); } await VehicleRepository.RefreshEntitiesForSiteAsync(site.Id, vehicles); importResult = new ImportVehiclesForSiteResult( site.Id, site.Name, vehicles.Count(), vehicles.Count(r => r.Condition == VehicleConditions.New), vehicles.Count(r => r.Condition == VehicleConditions.Used)); } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"No files in specified folder ({site.ImportRelativeFtpPath}).", ImportStatusEnum.NotStarted); } } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder ({site.ImportRelativeFtpPath}) was not found.", ImportStatusEnum.NotStarted); } } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder is not defined.", ImportStatusEnum.NotStarted); } } catch (Exception ex) { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, ex.Message, ImportStatusEnum.Failed); } return(importResult); }
public async Task TestUploadAndDownloadAsync() { await _client.UploadAsync( Encoding.UTF8.GetBytes("Hello, this is a test!"), "test.txt"); var temp = new MemoryStream(); await _client.DownloadAsync( temp, "test.txt"); var readData = Encoding.UTF8.GetString(temp.ToArray()); Assert.Equal("Hello, this is a test!", readData); }