简体   繁体   中英

File not being released after upload

Using Asp.Net MVC, I'm uploading a zip file and extracting it using the following method, and there seems to be a process still attached to it, as I cannot delete it in Windows Explorer after processing.

Here's the controller post:

[HttpPost]
public ActionResult ImportZip(HttpPostedFileBase zip)
{
    try
    {
        if (zip == null)
            throw new Exception("Please select a file to import");

        var result = new ContentManagementImporter(EducorDbRepo).ImportZipFile(zip);

        ViewData[ViewDataKeys.SuccessMessage] = result;
        return View("Import");
    }
    catch (Exception ex)
    {
        SetViewError(ex);
        return View("Import");
    }
}

And here's the method that process the uploaded file:

private static Dictionary<string, string> UnzipFiles(HttpPostedFileBase zipFile)
{
    var files = new Dictionary<string, string>();

    using (var archive = new ZipArchive(zipFile.InputStream))
    {
        foreach (var file in archive.Entries)
        {
            if (file == null)
                continue;

            using (var stream = file.Open())
            using (var reader = new StreamReader(stream))
            {
                var markup = reader.ReadToEnd();
                files.Add(file.Name, markup);
            }
        }
    }

    zipFile.InputStream.Close();

    return files;
}

Can you see where in the code I'm failing to close something that needs to be closed, or is there another reason?

The server behaviour can't lock files on the client. As such, I am 90% sure any locking you are seeing is caused by the browser itself (and thus outside your control).

You may want to consider trying an alternate browser and seeing what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM