public IActionResult DownloadFile(Guid id) { // For file downloads use FhFile // https://www.codeproject.com/Articles/1203408/Upload-Download-Files-in-ASP-NET-Core // https://www.c-sharpcorner.com/article/sending-files-from-web-api/ // PRE-CONDITION if (string.IsNullOrEmpty(id.ToString())) { return(BadRequest("file id is required")); } // verify file id exists var files = _filesService.GetFiles(); var file = files.FirstOrDefault(f => f.Id == id); if (file == null) { return(NotFound($"file with id is not found: {id}")); } // ACTIONS FileDownloadDto fileDownloadDto = _filesService.GetFileDownloadStreamById(id); if (fileDownloadDto == null) { return(NotFound()); } var contentType = FileHelpers.GetContentType(fileDownloadDto.FileName); var fileName = fileDownloadDto.FileName; var contentStream = fileDownloadDto.DownloadContentStream; return(File(contentStream, contentType, fileName)); }
public async Task <FileDownloadDto> DownloadFileAsync(string fileName) { var file = new FileDownloadDto(); try { var container = await GetContainer(); var blob = container.GetBlockBlobReference(fileName); using (var stream = new MemoryStream()) { await blob.DownloadToStreamAsync(stream); file.FileStream = stream.ToArray(); } await blob.FetchAttributesAsync(); var contentType = blob.Properties.ContentType; file.ContentType = contentType; file.Name = fileName; return(file); } catch (StorageException ex) { if (ex.RequestInformation.ErrorCode == BlobErrorCodeStrings.BlobNotFound) { throw new BlobNotFoundException(ex); } throw new GenericBlobStorageException(ex); } }
public When_OpportunityService_Is_Called_To_Get_Opportunity_Spreadsheet_Data() { var config = new MapperConfiguration(c => c.AddMaps(typeof(OpportunityMapper).Assembly)); var mapper = new Mapper(config); _opportunityRepository = Substitute.For <IOpportunityRepository>(); var opportunityItemRepository = Substitute.For <IRepository <OpportunityItem> >(); var provisionGapRepository = Substitute.For <IRepository <ProvisionGap> >(); var referralRepository = Substitute.For <IRepository <Domain.Models.Referral> >(); var googleMapApiClient = Substitute.For <IGoogleMapApiClient>(); _opportunityPipelineReportWriter = Substitute.For <IFileWriter <OpportunityReportDto> >(); var dateTimeProvider = Substitute.For <IDateTimeProvider>(); dateTimeProvider.UtcNow().Returns(new DateTime(2019, 03, 10)); _opportunityRepository.GetPipelineOpportunitiesAsync(Arg.Any <int>()) .Returns(new OpportunityReportDtoBuilder() .AddReferralItem() .AddProvisionGapItem() .Build()); var opportunityService = new OpportunityService(mapper, _opportunityRepository, opportunityItemRepository, provisionGapRepository, referralRepository, googleMapApiClient, _opportunityPipelineReportWriter, dateTimeProvider); _result = opportunityService.GetOpportunitySpreadsheetDataAsync(1) .GetAwaiter().GetResult(); }
public FileDownloadDto GetFileDownloadStreamById(Guid id) { // retrieve the file just to get the file name FhFile file; using (var db = _dbConnectionFactory.Open()) { var query = db.Select <FhFile>(f => f.Id == id); //SELECT by typed expression file = query.FirstOrDefault(); } // PRE-CONDITION // As a rule of thumb exception checks are done in the controller. // doing this here because we have to use the file.Name to buid the object if (string.IsNullOrEmpty(file?.Name)) { throw new Exception($"WARNING - FhFile with id doesn't exist: {id}"); } // build file to download, include stream and filename FileDownloadDto fileDownloadDto = new FileDownloadDto { FileName = file.Name, DownloadContentStream = GetFileDataStream(id), FileFullPath = GetFileFullPathById(id) }; return(fileDownloadDto); }
public async Task <IActionResult> Download(Guid id) { FileDownloadDto file = await Mediator.Send(new DownloadFileQuery() { Id = id }); return(File(file.Content, file.ContentType, file.Name)); }
public FileStreamResult FileDownload(FileDownloadDto fileDto, [FromServices] IWebHostEnvironment environment) { string foldername = ""; string filepath = Path.Combine(environment.WebRootPath, fileDto.FileUrl); var stream = System.IO.File.OpenRead(filepath); string fileExt = fileDto.FileUrl.Substring(fileDto.FileUrl.LastIndexOf('.')); // 这里可以写一个获取文件扩展名的方法,获取扩展名 //获取文件的ContentType var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(); var memi = provider.Mappings[fileExt]; var fileName = Path.GetFileName(filepath); return(File(stream, memi, HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8")))); }
public async Task ShouldReturnExistingFile(File file) { // Act await AddAsync(file); FileDownloadDto fileDto = await SendAsync(new DownloadFileQuery { Id = file.Id }); // Assert fileDto.Should().NotBeNull(); fileDto.Name.Should().Be(file.Name); fileDto.ContentType.Should().Be(file.ContentType); fileDto.Content.Should().Equal(file.Content); }