Convert physical file to iformfile keeps get null RRS feed

  • Question

  • User-139960961 posted

    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?


            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)
                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>();
                await _service.saveFile(files, testAssetsFolder, "upload", default);


            public async Task saveFile(List<IFormFile> files, string directory, string subDirectory, CancellationToken cancellationToken)
                subDirectory = subDirectory ?? string.Empty;
                var target = Path.Combine(directory, subDirectory);
                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);
                            // 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.

    Friday, July 3, 2020 6:12 AM

All replies

  • User-474980206 posted

    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.

    Friday, July 3, 2020 2:28 PM
  • User-139960961 posted

    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. 

    Friday, July 3, 2020 3:33 PM