I am trying to read a physical excel file to a IFormFile but the content is always empty when I am reading the file it keep shows byte 0. I am trying to test a method that will read the content of the excel file. Is there anyone that did testing that manage
to read an excel file to a iformfile or how do I write unit testing that is reading the file using epplus?
ServiceTest.cs
[Fact]
public async Task saveFile_test()
{
string rootPath = Directory.GetCurrentDirectory();
string testAssetsFolder = @"Assets\";
string testFile = "test.xlsx";
string testFilePath = Path.Combine(rootPath, testAssetsFolder, testFile);
// Arrange.
var fileMock = new Mock<IFormFile>();
var physicalFile = new FileInfo(testFilePath);
var ms = new MemoryStream();
var writer = new StreamWriter(ms);
using (FileStream fs = physicalFile.OpenRead())
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
{
writer.WriteLine(temp.GetString(b));
}
}
writer.Flush();
ms.Position = 0;
var fileName = physicalFile.Name;
//Setup mock file using info from physical file
fileMock.Setup(_ => _.FileName).Returns(fileName);
fileMock.Setup(_ => _.Length).Returns(ms.Length);
fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
fileMock.Setup(_ => _.ContentDisposition).Returns(string.Format("inline; filename={0}", fileName));
List<IFormFile> files = new List<IFormFile>();
files.Add(fileMock.Object);
await _service.saveFile(files, testAssetsFolder, "upload", default);
}
Service.cs
public async Task saveFile(List<IFormFile> files, string directory, string subDirectory, CancellationToken cancellationToken)
{
subDirectory = subDirectory ?? string.Empty;
var target = Path.Combine(directory, subDirectory);
Directory.CreateDirectory(target);
foreach (IFormFile file in files)
{
if (file.Length <= 0) return;
var filePath = Path.Combine(target, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream, cancellationToken);
try
{
// add to read encoded 1252 values
using (var package = new ExcelPackage(stream))
{
// Express worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets["Express"];
int colCount = worksheet.Dimension.End.Column; //get Column Count
int rowCount = worksheet.Dimension.End.Row;
I am out of clue how I should write a unit test as when I debug I am able to mock the file as Imockfile reading from an existing file. But when the test run to ExcelWorksheet worksheet = package.Workbook.Worksheets["Express"]; it
just shows null.
The copy stream moves the stream to end of file, so when you pass the stream to the ExcelPackage, and it reads it, it gets no data. To need to reset (if supported, will be work with actual file stream, probably not with actual network stream) or pass new
stream of the copied file.
Thank you for your response. I thought the behaviour was that too. When I went to check the file copied over "copy stream" the file contains 0 bytes too.
Member
9 Points
55 Posts
Convert physical file to iformfile keeps get null
Jul 03, 2020 06:12 AM|weihan1394|LINK
I am trying to read a physical excel file to a IFormFile but the content is always empty when I am reading the file it keep shows byte 0. I am trying to test a method that will read the content of the excel file. Is there anyone that did testing that manage to read an excel file to a iformfile or how do I write unit testing that is reading the file using epplus?
ServiceTest.cs
Service.cs
I am out of clue how I should write a unit test as when I debug I am able to mock the file as Imockfile reading from an existing file. But when the test run to
ExcelWorksheet worksheet = package.Workbook.Worksheets["Express"];
it just shows null.All-Star
58124 Points
15640 Posts
Re: Convert physical file to iformfile keeps get null
Jul 03, 2020 02:28 PM|bruce (sqlwork.com)|LINK
The copy stream moves the stream to end of file, so when you pass the stream to the ExcelPackage, and it reads it, it gets no data. To need to reset (if supported, will be work with actual file stream, probably not with actual network stream) or pass new stream of the copied file.
Member
9 Points
55 Posts
Re: Convert physical file to iformfile keeps get null
Jul 03, 2020 03:33 PM|weihan1394|LINK
Hi @bruce (sqlwork.com),
Thank you for your response. I thought the behaviour was that too. When I went to check the file copied over "copy stream" the file contains 0 bytes too.