How to troubleshoot ASP.NET Core 6 MVC view problem when using ZipFile

50 Views Asked by At

I have a simple ASP.NET Core 6 MVC web app to test viewing various files. The tests were performed in Visual Studio 2022 on localhost.

The test steps include:

  1. Using a sample file example03.docx and save it in .htm format.
  2. It created 2 items example03.htm and a folder example03_files
  3. Copied the file and folder to /wwwroot/files folder
  4. In the app, in the HomeController, add an action method ViewWordFile (see CODE #1)
  5. Created a view ViewWordFile.cshtml (see CODE #2)
  6. Test the code and everything worked as expected

I encountered problems after trying to compress the example03.htm and related folder into example03.zip file. I then tried to unzip that file in a ViewWordFile action method (see details in problem description section below)

CODE #1:

public IActionResult ViewWordFile()
{
    return View();
}

CODE #2:

@{
    ViewData["Title"] = "View Office File";
}

<div class="container">
    <h3>View MS Word Document</h3>
    <p>
    <div class="row text-center">
        <iframe src='https://localhost:7131/files/example03.htm' width='100%' height='650px' frameborder='0'></iframe>
    </div>
    </p>
</div>

Problem description:

  1. Added 2 subfolders /wwwroot/files/zipped and /wwwroot/files/unzip
  2. Manually created zip file example03.zip and copied it to the /files/zipped folder
  3. In order to decompress the zip file, I added a method unpackFile in the HomeController (see CODE #3)
  4. Modify the ViewWordFile action method (see CODE 4)
  5. When trace the code during execution, the unzip worked fine and unzipped files were placed in the /files/unzip folder
  6. After these changes, the ViewWordFile action stopped working. During tracking, I could see the view was activated, when I put a breakpoint in the code block at the top. But, nothing was displayed. It looked like it was still at home page.
  7. I thought it might be a file permission issue. I removed code calling UnpackFile in ViewWordFile action (as in CODE #3) and keep using files in /unzip folder. The page started working again. It showed file permission was not the actual problem.
  8. I suspect there was something with calling ZipFile.ExtractToDirectory. I changed code to call this method inside of ViewWordFile action (see CODE #5). The problem happened again and page stopped working.

It seemed that the ZipFile method interfered with the view. But I was unable to figure out the actual reason.

Any help or suggestions for troubleshooting will be greatly appreciated.

CODE #3

private bool UnpackFile (string zipFile, string unzipDir)
{
    bool isOk = false;

    if (System.IO.File.Exists(zipFile))
    {
        ZipFile.ExtractToDirectory(zipFile, unzipDir, true);
        isOk = true;
    }

    return true;
}

CODE #4

public IActionResult ViewWordFile()
{
    string zipFile = Path.Combine(_hostEnv.WebRootPath, "files/zipped", "example03.zip");
    string unzipDir = Path.Combine(_hostEnv.WebRootPath, "files/unzip");
    UnpackFile(zipFile, unzipDir);

    return View();
}

CODE #5

public IActionResult ViewWordFile()
{
    string zipFile = Path.Combine(_hostEnv.WebRootPath, "files/zipped", "example03.zip");
    string unzipDir = Path.Combine(_hostEnv.WebRootPath, "files/unzip");
    ZipFile.ExtractToDirectory(zipFile, unzipDir, true);

    return View();
}
0

There are 0 best solutions below