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); }
private async Task <BinaryDto> SaveFileAsync(IFormFile file) { if (file.Length > 0) { BinaryDto dto = await CreateFileEntity(file); await SaveAsync(dto.Id, file.OpenReadStream()); return(dto); } return(null); }
private BinaryDto ToDto(Binary binary) { BinaryDto result = new BinaryDto() { Id = binary.Id, FileName = binary.FileName, Name = binary.Name, Length = binary.Length, ContentType = binary.ContentType, ContentDisposition = binary.ContentDisposition, }; return(result); }
public async Task <IList <BinaryDto> > UploadFilesAsync(IList <IFormFile> files) { IList <BinaryDto> uploadedFiles = new List <BinaryDto>(); foreach (IFormFile formFile in files) { BinaryDto uploadedFile = await SaveFileAsync(formFile); if (uploadedFile != null) { uploadedFiles.Add(uploadedFile); } } return(uploadedFiles); }
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); }
public async Task <int> AddBinaryAsync(BinaryDto binaryDto) { Binary binary = new Binary(); binary.Length = binary.Length; binary.ContentType = binary.ContentType; binary.FileName = binary.FileName; binary.ContentDisposition = binary.ContentDisposition; binary.Name = binary.Name; binary.User = await _userService.GetOrCreateCranUserAsync(); binary.IdUser = binary.User.Id; _dbContext.Binaries.Add(binary); await _dbContext.SaveChangesAsync(); return(binary.Id); }
private async Task <BinaryDto> CreateFileEntity(IFormFile formfile) { Binary fileEntity = new Binary(); fileEntity.Length = (int)formfile.Length; fileEntity.ContentType = formfile.ContentType; fileEntity.FileName = formfile.FileName; fileEntity.ContentDisposition = formfile.ContentDisposition; fileEntity.Name = formfile.Name; fileEntity.User = await _userService.GetOrCreateCranUserAsync(); fileEntity.IdUser = fileEntity.User.Id; _dbContext.Binaries.Add(fileEntity); await _dbContext.SaveChangesAsync(); BinaryDto dto = ToDto(fileEntity); return(dto); }