public async Task <FileStreamResult> GetFile(int id) { BinaryDto fileInfo = await _binaryService.GetFileInfoAsync(id); Stream stream = await _binaryService.GetBinaryAsync(id); var result = File(stream, fileInfo.ContentType, fileInfo.FileName); return(result); }
public async Task SaveBinaryTest() { TestingContext testingContext = new TestingContext(); testingContext.AddAdminPrincipalMock(); testingContext.AddLogServiceMock(); testingContext.AddRealDb(); testingContext.AddUserService(); IBinaryService service = testingContext.GetService <BinaryService>(); string testWord = "Hello World"; var bytes = System.Text.Encoding.UTF8.GetBytes(testWord); MemoryStream stream = new MemoryStream(); stream.Write(bytes, 0, bytes.Length); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); BinaryDto binaryDto = new BinaryDto { ContentDisposition = "ContentDisposition", ContentType = "ContentType", FileName = "FileName", Name = "Name", Length = 2334, }; int id = await service.AddBinaryAsync(binaryDto); //Act await service.SaveAsync(id, stream); //Assert Stream streamToAssert = await service.GetBinaryAsync(id); StreamReader reader = new StreamReader(streamToAssert); string text = reader.ReadToEnd(); Assert.Equal(testWord, text); //Cleanup await service.DeleteBinaryAsync(id); }
private async Task ExportBinaries(ZipArchive zip) { IQueryable <int> questionList = GetQuestionIds(); var result = await _context.RelQuestionImages .Where(x => questionList.Contains(x.Question.Id)) .Select(x => new { BinaryId = x.Image.Binary.Id, FileName = x.Image.Binary.FileName, }).ToListAsync(); foreach (var item in result) { string fileName = $"{item.BinaryId}_{item.FileName}"; ZipArchiveEntry entry = zip.CreateEntry(fileName); using (Stream entryStream = entry.Open()) { Stream fileStream = await _binaryService.GetBinaryAsync(item.BinaryId); await fileStream.CopyToAsync(entryStream); } } }